2

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.

michiel
  • 6,036
  • 1
  • 18
  • 13
  • I find this question interesting, but I am left wondering what makes it necessary. It seems that the model should abstract it so you don't need to do it this way. – Sonny Jun 03 '10 at 17:42

1 Answers1

0
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
  • 55,990
  • 32
  • 132
  • 223