views:

19

answers:

1
+2  Q: 

row specific class

How do I create a Zend_Db_Table which returns a different class for each row.?

Example UserTable has id,name and type Type contains class names (admin,client,etc...)

The classes admin, client are all subclasses of user

If I call fetch I need to get a admin or client object depending on the corresponding value in the db.

A: 
class Your_Db_Table_Row extends Zend_Db_Table_Row_Abstract
{
}

class Your_Db_Table extends Zend_Db_Table_Abstract
{
    protected $_rowClass = "Your_Db_Table_Row";
}

or

new Your_Db_Table(array("rowClass" => "Your_Db_Table_Row");

So whenever you get a rowset from your table subclass, the rows included in it will be your custom class.

Edit

To get a custom row based on a value, I would say extend the Zend_Db_Table_Rowset_Abstract class instead and override this method:

getRow(int $position, [bool $seek = false])

You'll also need to override the current method and perhaps some of the other SeekableIterator implemetations which actually creates a row class based on the _rowClass property. You might be able to set the _rowClass before current is called based on your data's row type.

You could instantiate a specific class in current and return it based on the type parameter.

Have you though about maybe just using composition instead? Say just passing in data to a new class if it's an admin type or something?

Typeoneerror
This will use the 'Your_Db_Table_row' class for each row, it needs to be different depeding on the 'type' field value.
stimpie
Very true; I read that question too quickly. Edited with a suggestion.
Typeoneerror