Loading Please wait...

Advanced ORM Usage.

You must read Object Relation Maping Page before reading this.
This documentation will explain some advanced usage of the ORM layer.

Row Level Operation(s)

After fetching a row you generally want to extract values of Columns from it using getCol() methods.However you can do even more, you can even update or delete a Row.


To Update a field in the Fetched row You specify New values to the Columns using setCol() methods e.g. If you have a Column called Counter You can execute $row->setCounter($row->getCounter()+1) to specify the new value to that Column.


after specifying all the set's if you Invoke update that row will be updated. You need to pass a reference of tabl level Object to this method.e.g. $row->save($this->db->Student) (For PHP4 Compatiability)


If you want you can delete that remove or dete that Ro too. to do that just invoke $row->delete($this->db->Goods). remove method is an alias of delete method.

Custom Methods

As both table level and Row level Classes are after all general PHP Classes they can be modified to tune their behavior.

Table Level

a table level ORM Class e.g. assume Student is instantiated into $this->db as $this->db->Student. so if we add some Custom method on Student Class we can invoke that from $this->db->Student in our controller.

assume our Student table has a firstName and a lastName Column. so get get the Name of the student who's Id is 1 we need to execute $this->db->Student->findFirstNameById(1)." ".$this->db->Student->findLastNameById(1). But it would be better and easier too if you could use $this->db->Student->findNameById(1). But remember there is no such Column called name in our table.

To handle this situation you can add a method in your Student Class.

public function findNameById($id){
  return $this->findFirstNameById($id)." ".$this->findLastNameById($id);
and now you can execute $this->db->Student->findNameById(1) which will return the Name of the student who's id is 1.

Row Level

Assume you have invoked $stdRowObj = $this->db->Student->findAllByPk(1) now $stdRowObj is a Row Level object of Student table. so to get FirstName from it you can use $stdRowObj->getFirstName() and to get Last Name you can use $stdRowObj->getLastName(). But there is no such column Called Name. so to get Full Name you can use $stdRowObj->getFirstName()." ".$stdRowObj->getLastName(). But It would be better if you can use just $stdRowObj->getName() Instead.

As $stdRowObj is an Instance of Student Class's row Level Class you can modify the StudentRowSet Class and add an extra method to it called getName().

public function getName(){
  return $this->getFirstName()." ".$this->getLastName();
so that whenever you call $stdRowObj->getName() the Custom method will be invoked.


Triggers are method that gets called automatically when doing any CRUD Operation. e.g. while doing some insert operation the trigger for insert method will be called automatically.triggers are custom methods that are added in the Table level Class.

Insert Trigger

if you have a method called onInsert() that method will be called just before an insert operation. the signature of teh function is onInsert($row)the argument is optoional. Zigmoyd supplies a reference of an associative array of columns and their corresponding values to he method. You just need to change the values of the array if you want to do some final changes.

If you have a method onInsertCol($val) that will be called just before inserting the date. e.g. If you have a Column called Email. and you want emails will always be stored as user at somesite dot com format rathar than storing it raw.

You can create an onInsertEmail($val) Trigger like the following.

public function onInsertEmail($val){
  $val = str_replace(array('@', '.'), array(' at ', ' dot '), $val);
which will be triggered just before inserting some data on Email Column.

If you have both RowLevel and column level Insert trigger both will be called. first the row level will be called and then column level will be calld.

Update Trigger

Similer to Insert trigger. here the method names will be onUpdate($row) and onUpdateCol($val) e.g if teh Column name is Email the trigger for that Column will be onUpdateEmail($value)

Delete Trigger

If you have an onDelete() method it will be triggered just before any deleta Operation.

Export Trigger

Whenever you retrive some data from a table, doesn't matter its find or fetch or something else. its first exported and then further formated if necessary. so If you want to reformat some data just before sending it to you can use Export Trigger. e.g. assume you have a Column Email which contains emails in user@somesite.com format. Now what you want is whenever you retrive some data it will be in user at somesite dot com you can simply create a trigger onExportEmail($val) like this.
public function onExportEmail($val){
  $val = str_replace(array('@', '.'), array(' at ', ' dot '), $val);
so whenever some Emailaddress is fetched it will be in AntiSpam format. You can also use Row Level triggers for Export too like onExport($assoc), that accepts an associative array of key value pair. You just update some value there and it will reflect.

Generated on Mon Oct 27 23:51:59 2008 for zigmoyd.kdevelop by doxygen 1.5.6