views:

133

answers:

2

I have a model which is referenced to a table registries extending Zend_Db and some methods which bascially uses fetchAll() all the time.

What happens is: my table has a DATE field and I'd like it to output already formated when I call $row->reg_date. Is it possible by doing something in the model or do I have to manually format?

+1  A: 

You've got a couple of options:

  • Overwrite Zend_Db_Table_Row_Abstract::__get() in your table when asking for the reg_date
  • Format it in the view with your own view helper, something like <?= $this->formatDate($this->row->reg_date, 'fancy'); ?>

Doctrine probably has the answer you would like, the following is cut from http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-models#generating-models:

class User extends BaseUser
{
    public function setPassword($password)
    {
        return $this->_set('password', md5($password));
    }
}

$user = new User();
$user->username = 'jwage';
$user->password = 'changeme';

echo $user->password; // outputs md5 hash and not changeme

If I were you, I'd go with the view helper-option. You could add custom CSS there and have it all ready to go. If you'd need it changed, you'd change it in one place and your application's renewed.

Besides, if you ever wanted to do something with the date (compare it to another date, get just the month, localize it, etc.) you want a clean starting point which is what you'd store in the database (i.e. 2010-05-16 or 2010-05-16 16:55:00). Therefore: view settings belongs in the view.

chelmertz
Thxxx for the help!
Rodrigo Alves
A: 

I would strongly recommend against formatting a date that way. Your requirement for date formatting might change at some point down the road, requiring you to make changes to custom db access classes. Custom formatting of this type should really be done in the view, IMHO.

If you just have to do it (and I've done stuff like this before, regretting it later), you'll want to extend Zend_Db_Table_Row and do your custom formatting there. Placing your custom formatting in a subclass will mitigate the damage down the road ;) Like chelmertz said, you'll likely override __get(), check for reg_date, and applying custom formatting at that point.

Jeremy Kendall