Re: DB_DataObject and Templates
| From: | Michiel van der Blonk | Date: | Mon, 06 Dec 2004 21:21:51 +0000 |
| Subject: | Re: DB_DataObject and Templates | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-16057@lists.php.net to get a copy of this message | ||
Hi
Instead of using
while ($table->fetch()) {
$data[] = $table->toArray();
}
in a separate function it would be better to subclass DB_DataObject and
write a function like this one
function toArray($linked = false) {
// select all
$this->find();
// create output array
$ret = array();
// loop through records
while ($this->fetch()) {
if ($linked)
$this->getLinks();
$ret[$this->getId()] = $this->__clone();
}
// return array
return $ret;
}
You can then simply use
$smarty->assign("foo", $Foo->toArray()); // only $FOO
or
$smarty->assign("foo", $Foo->toArray(true)); // include related
'lookup'
records
You can also write functions for getting a hierarchical set of data starting
at a top level. For instance, get all categories and their products in an
array of DataObjects, where each category also has an array of Product
DataObjects. This helps the designer alot, since they have a simple starting
point and can traverse the 'tree' downwards.
grtz
Michiel
"Michael Wallner" <mike@php.net> wrote in message
news:20041201120943.6996.qmail@pb1.pair.com...
"Michael Wallner" <mike@php.net> wrote in message
news:20041201120943.6996.qmail@pb1.pair.com...