DataObject & calculated fields
| From: | Norbert Mocsnik | Date: | Fri, 23 Jan 2004 21:02:13 +0000 |
| Subject: | DataObject & calculated fields | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-25239@lists.php.net to get a copy of this message | ||
Hi again,
Yesterday I was thinkin about DataObject and calculated fields..
I know calcfields from Delphi and find them really useful. The main idea is very simple: let the developer be able to create new fields in any table, from code. The value of this field is calculated on-the-fly after fetching a record. Imagine a simple scenario about user profiles:
users
-----
userid
first_name
last_name
..
You can create a (virtual) field from code called full_name which contains the concatenation of first and last name fields. I did the following:
1. added a new function to the DataObject core:
function _calcFields() {
if (method_exists($this, 'calcFields')) {
$this->calcFields();
}
}
2. at the end of fetch() (before "return true;") I call:
$this->_calcFields();so my function will be called every time after fetching a record. * Note: I'm not sure about where to place this call. I'm not sure this will be called after every fetch because there are fetches when the fetch() function itself won't be called. It would be nice if someone could help me to find the points to call it from. After that, it's very simple to create calculated fields in DataObject-based classes:
function calcFields() {
$this->full_name = $this->first_name.' '.$this->last_name;
.. or another example ..
$this->full_price = $this->price * $this->vat;
.. or another example ..
$this->full_price = $this->price * $this->taxes;
return true;
}
I wonder if you like this..
Regards,
Norbert