Rob Allen, Nick Lo, and Steven Brown
"Zend Framework in Action"
7.
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.manning-sandbox.com/forum.jspa?forumID=329
These are ???behind the scenes??? details that the rest of the system doesn??™t need worry about and so ideal for
placing in the model.
Listing 2.7: Automatically maintaing date fields in a model
class News extends Zend_Db_Table
{
protected $_name = 'news';
public function insert($data)
{
if (empty($data['date_created'])) {
$data['date_created'] = date('Y-m-d H:i:s');
}
return parent::insert($data); #1
}
public function update($data)
{
if (empty($data['date_updated'])) { |#2
$data['date_updated'] = date('Y-m-d H:i:s'); |
} |
return parent::update($data);
}
}
(annotation) <#1 Zend_DB_Table??™s insert() function continues to do the real work.>
(annotation) <#2 Only set the date field if it??™s not already be seen by the caller.>
This code is self-explanatory. When inserting, if date_created hasn??™t already been supplied by the caller,
we fill in today??™s date and then call Zend_Db_Table??™s insert() function. For updating, the story is similar,
except that we change the date_updated field instead.
We can also write our own functions for retrieving data according to the business logic required by the
application.
Pages:
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78