Issues

ZF-1144: Support default table in constructor of custom Row

Description

Jeff Bailey writes:

I have a Class that I'm inheriting from Zend_Table_Db_Row_Abstract to use as a customClass in a table, let's call it User. This works fine for retrieving rows and giving me all of my attached logic, update checks and whatnot.

What I'd like to be able to do is use it naturally for creating empty objects, so


$aUser = new User();

In order to do this, I've cooked up a constructor that will work:


class User extends Zend_Db_Table_Row_Abstract
{
    private $_tableName = "UserTable";

    public function __construct(array $config = array())
    {
        if (empty($config["table"])) {
            $config["table"] = new $this->_tableName();
            $info = $config["table"]->info();
            $keys = array_values($info[Zend_Db_Table_Abstract::COLS]);
            $vals = array_fill(0, count($keys), NULL);
            $config["data"] = array_combine($keys, $vals);
        }

        parent::__construct($config);
    }
}

I think that this type of logic would be nice to have as the default constructor for Zend_Db_Table_Row_Abstract.

Comments

I've used the fetchNew method, but then it means that the business logic needs to know about the table subclasses.


$userTable = new UserTable();
$aUser = UserTable->fetchNew();

feels less natural than:


$aUser = new User();

and also makes for code that is slightly easier to test with null objects.

We should allow primary key fields to be set. This issue would be much easier to solve if that were true. So I'm marking it as a dependency.

Please categorize/fix as needed.

This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.

Resolved in r.13552