Details
-
Type:
New Feature
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 0.9.1
-
Fix Version/s: 1.8.0
-
Component/s: Zend_Db_Table
-
Labels:None
-
Fix Version Priority:Nice to Have
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.
Issue Links
| This issue depends on: | ||||
| ZF-191 | Allow setting primary key values when creating/updating rows |
|
|
|
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.
$userTable = new UserTable(); $aUser = UserTable->fetchNew();$aUser = new User();