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
Posted by Bill Karwin (bkarwin) on 2007-03-27T13:53:19.000+0000
I've used the fetchNew method, but then it means that the business logic needs to know about the table subclasses.
feels less natural than:
and also makes for code that is slightly easier to test with null objects.
Posted by Bill Karwin (bkarwin) on 2007-04-17T17:55:45.000+0000
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.
Posted by Wil Sinclair (wil) on 2008-03-25T20:43:58.000+0000
Please categorize/fix as needed.
Posted by Wil Sinclair (wil) on 2008-04-18T13:12:06.000+0000
This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Posted by Jurrien Stutterheim (norm2782) on 2009-01-08T06:53:31.000+0000
Resolved in r.13552