Zend_Db_Table_Row: Why do I need to use createRow ()?

I am wondering why in Zend_Db you need to use the createRow method to get a new empty Row object, and not just instantiate the Row object directly.

For example, if I extended the default classes to create my own user and class tables, then to create a new row object, I always need to do the following to create a new Row object:

$userTable = new App_Table_User(); $userRow = $userTable->createRow(); $userRow->setName('bob'); $userRow->save(); 

But for me it makes sense to just say:

 $userRow = new App_Row_User(); $userRow->setName('bob'); $userRow->save(); 

I understand that I can always create this functionality in my Row classes extending Zend_Db_Table_Row_Abstract , but I wonder if there is any specific reason why the Zend Framework team didn't just do this by default?

+4
source share
1 answer

You can instantiate the Row class directly with new .

But the Row object needs to know which columns are valid for this row of data.

There is no magic to match the row class name with the corresponding table class name. Therefore, the row itself does not know which table it belongs to. Therefore, he cannot guess how to write SQL to perform insert or update operations; it gets this metadata from the corresponding Zend_Db_Table object. You must specify the row in which the table should be used.

If you create a row object with new , you must pass the configuration array to the constructor, including the name of the table class, or an instance of the object of this table class.

 $userRow = new App_Row_User( array('table' => 'App_Table_User') ); 

or

 $userTable = new App_Table_User(); $userRow = new App_Row_User( array('table' => $userTable) ); 

Without a valid table name or class name, instantiating a Row object with new will throw an exception.

Similarly, save() uses a table object by calling the insert() or update() methods. Without a valid table object, the Row save() method throws an exception.

+4
source

All Articles