Re: toArray was DB_DataObjects and generating search filters
| From: | Laszlo Hermann | Date: | Mon, 25 Apr 2005 07:17:39 +0000 |
| Subject: | Re: toArray was DB_DataObjects and generating search filters | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-18939@lists.php.net to get a copy of this message | ||
Yes, you're right; this was quite a bad example. I agree that the way is
building the DO, then find() and fetching() it. As for the huge DO array, as
you said, sometimes useful but this is a lot nicer:
while ($this->fetch()) {
$list[] = $this->toArray();
}
On 4/22/05, Justin Patrin <papercrane@gmail.com> wrote:
> On 4/21/05, Laszlo Hermann <laszlo.hermann@gmail.com> wrote:
> > I've added the following function to my DB_DataObject:
> >
> > function toArrayList ()
> > {
> > $list = array();
> > while ($this->fetch()) {
> > $list[] = $this;
> > }
> > return $list;
> > }
> >
> > This is pretty useful for me. In the program I just write
> >
> > $person = DB_DataObject::factory('person');
> > $query = "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.
>
> >
> > 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.
> >
> > Laszlo.
> >
> >
> > On 4/22/05, Justin Patrin <papercrane@gmail.com> wrote:
> > > On 4/21/05, Dan Rossi <pear@electroteque.org> wrote:
> > > >
> > > > On 22/04/2005, at 2:56 AM, Justin Patrin wrote:
> > > >
> > > > > On 4/20/05, pear@electroteque.org <pear@electroteque.org> wrote:
> > > > >> Ok this is how i currently have to setup a method to return an
array
> > > > >> for
> > > > >> list menus as u can see the code is duplicating. toArray returns
keys
> > > > >> as
> > > > >> 0,1,2,3,4,5,6,7 etc ...
> > > > >>
> > > > >> keys need to be the primary keys :|
> > > > >>
> > > > >> function getDepartments()
> > > > >> {
> > > > >> $this->orderBy('department');
> > > > >> $this->find();
> > > > >> $arr = array();
> > > > >> $arr[''] = "";
> > > > >> while ($this->fetch()) {
> > > > >> $arr[$this->departmentID] = $this->department;
> > > > >> }
> > > > >> return $arr;
> > > > >> }
> > > > >>
> > > > >
> > > > > Did you have a question?
> > > > >
> > > > > --
> > > > >
> > > >
> > > > LOL yes , is there a more formulated way already without having to
> > > > duplicate code ?
> > > >
> > >
> > > Well...I'm not sure what you're asking here. toArray() returns an
> > > assoc array of the current record, not a list of records... and it
> > > certainly doesn't have numbered indexes.
> > >
> > > Which code are you supposedly duplicating?
> > >
> > > --
> > > Justin Patrin
>
> --
> Justin Patrin
>