Automatically selecting dates from databases as Zend_Date objects?

I've encountered scenarios where I've wanted to do this. Here is the solution that I've used: Created an extended row class for Zend_Db_Table_Row and overloaded the __get() and __set() super-methods In the specific classes/tables that I want to use date objects, created the appropriate methods to do the heavy lifting Here is a dumbed-down version of the extended row class that I use on my projects: category FireUp * @package FireUp_Db * @copyright Copyright (c) 2007-2009 Fire Up Media, Inc. (fireup.net) * @license dev.fireup.net/license/mit MIT License * @uses Zend_Db_Table_Row */ class FireUp_Db_Table_Row extends Zend_Db_Table_Row_Abstract { /** * Retrieve row field value * * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists * * @param string $columnName The user-specified column name.

* @return string The corresponding column value. * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row. */ public function __get($key) { $inflector = new Zend_Filter_Word_UnderscoreToCamelCase(); $method = '_get' .

$inflector->filter($key); if (method_exists($this, $method)) { return $this->{$method}(); } return parent::__get($key); } /** * Set row field value * * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists * * @param string $columnName The column key. * @param mixed $value The value for the property. * @return void * @throws Zend_Db_Table_Row_Exception */ public function __set($key, $value) { $inflector = new Zend_Filter_Word_UnderscoreToCamelCase(); $method = '_set' .

$inflector->filter($key); if (method_exists($this, $method)) { return $this->{$method}($value); } return parent::__set($key, $value); } } For our individual table classes, we override the functions as such: class EntityRecord extends FireUp_Db_Table_Row { protected function _getDateCreated() { return new Zend_Date($this->_data'date_created', Zend_Date::ISO_8601); } protected function _setDateCreated($value) { if ($value instanceof Zend_Date) { $value = $value->toString('YYYY-MM-dd HH:mm:ss'); } $this->_data'date_created' = $value; $this->_modifiedFields'date_created' = true; } } Now, creating a new Zend_Date object everytime that the field would be accessed has some overhead, so in our classes, we take additional measures to cache the date objects, etc, but I didn't want that to get in the way of showing you the solution.

I've encountered scenarios where I've wanted to do this. Here is the solution that I've used: Created an extended row class for Zend_Db_Table_Row and overloaded the __get() and __set() super-methods In the specific classes/tables that I want to use date objects, created the appropriate methods to do the heavy lifting Here is a dumbed-down version of the extended row class that I use on my projects: /** * @category FireUp * @package FireUp_Db * @copyright Copyright (c) 2007-2009 Fire Up Media, Inc. (fireup.net) * @license dev.fireup.net/license/mit MIT License * @uses Zend_Db_Table_Row */ class FireUp_Db_Table_Row extends Zend_Db_Table_Row_Abstract { /** * Retrieve row field value * * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists * * @param string $columnName The user-specified column name.

* @return string The corresponding column value. * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row. */ public function __get($key) { $inflector = new Zend_Filter_Word_UnderscoreToCamelCase(); $method = '_get' .

$inflector->filter($key); if (method_exists($this, $method)) { return $this->{$method}(); } return parent::__get($key); } /** * Set row field value * * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists * * @param string $columnName The column key. * @param mixed $value The value for the property. * @return void * @throws Zend_Db_Table_Row_Exception */ public function __set($key, $value) { $inflector = new Zend_Filter_Word_UnderscoreToCamelCase(); $method = '_set' .

$inflector->filter($key); if (method_exists($this, $method)) { return $this->{$method}($value); } return parent::__set($key, $value); } } For our individual table classes, we override the functions as such: class EntityRecord extends FireUp_Db_Table_Row { protected function _getDateCreated() { return new Zend_Date($this->_data'date_created', Zend_Date::ISO_8601); } protected function _setDateCreated($value) { if ($value instanceof Zend_Date) { $value = $value->toString('YYYY-MM-dd HH:mm:ss'); } $this->_data'date_created' = $value; $this->_modifiedFields'date_created' = true; } } Now, creating a new Zend_Date object everytime that the field would be accessed has some overhead, so in our classes, we take additional measures to cache the date objects, etc, but I didn't want that to get in the way of showing you the solution.

1 This assumes that you are using Zend_Db_Table and specifying the row class for the model – gmcrist Oct 30 '09 at 11:18 I would do it the same way. You can do the same thing in any other persistence layer other the nZend_Db_Table ;) – TomáÅ¡ Fejfar Oct 30 '09 at 12:57 Great answer with code samples! – David Caunt Oct 30 '09 at 21:01.

Use Zend_Table and have your table return you custom row objects that extend Zend_Db_Table_Row_Abstract. Then just have a method on that row like function getDate() { return new Zend_Date($this->datecol, Zend_Date::TIMESTAMP); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions