For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > April 2005 > Re: [PEAR] toArray was DB_DataObjects and generating search filters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR] toArray was DB_DataObjects and generating search filters
Justin Patrin

2005-04-22, 3:58 pm

On 4/21/05, Laszlo Hermann <laszlo.hermann@gmail.com> wrote:
> I've added the following function to my DB_DataObject:
>=20
> function toArrayList ()
> {
> $list =3D array();
> while ($this->fetch()) {
> $list[] =3D $this;
> }
> return $list;
> }
>=20
> This is pretty useful for me. In the program I just write
>=20
> $person =3D DB_DataObject::factory('person');
> $query =3D "SELECT ... foo ... bar...";
> $person->query($query);
> $smartyRenderer->setAttribute('persons', $person->toArrayList());


This is basically no different than:

$smartyRenderer->setAttribute('persons', $db->getAll($query));

The only thing you're really gaining here (unless you're using the DO
for more than data storage in yoru Smarty Template which I hope you're
not) is the ability to override fetch() to do data manipulation and
such.

In other words, you're *not really using DB_DataObject here*. You're
using it as a wrapper for DB which basically just slows you down.

1) You're not supposed to write SQL with DB_DO. You *may* have to
write snippets and whereAdd or selectAdd, but you shouldn't be writing
the whole SQL.
2) You're meant to use the DB_DO data as you're fetching it. Making
one huge array of DOs is useful sometimes, but in general is just
going to use up lots of memory.
3) If all you're doing is fetching a bunch of records and making them
into arrays you might as well be using DB directly. You're just
including extra overhead by makign DB_DO do what DB already does.
[color=darkred]
>=20
> So first you build the dataobject, write the find / get / query, then
> just call toArrayList() then you have a list of the DOs. This works
> for very well for me; however, I would appreciate if the original
> DB_DataObject would have this, written in a more professional way.
>=20
> Laszlo.
>=20
>=20
> On 4/22/05, Justin Patrin <papercrane@gmail.com> wrote:
ay[color=darkred]
eys[color=darkred]
tment;[color=darkred]


--=20
Justin Patrin
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com