Thursday, July 30, 2009

Re: Efficient ways to retrieve data from models with high association depth

On Jul 30, 9:00 pm, Roel <roel....@gmail.com> wrote:
> I've tried Containable but it doesn't help lower the number of queries
> made. In fact in some circumstances Containable creates unnecessary
> queries (https://trac.cakephp.org/ticket/5864).
>
> To be clear, I have no trouble getting my data. It's just, from a
> perspective of someone who handwritten his MySQL queries, CakePHP
> could be much more efficient with its database calls.
>
> At points I'm a bit torn between writing an app that prioritizes
> efficient database queries or Cake's logic. To illustrate:
>
> Option 1:
>
> $data = $this->Article->read();
>
> <queries made (aside from DESCRIBE): 1 + [num_comments]>
>
> Option 2:
>
> $this->Article->recursive = -1;
>
> $articleData = $this->Article->read();
>
> $postData = $this->Article->Comment->find('all', array('conditions' =>
> array('Comment.article_id' => $this->Article->id)));
>
> <queries made (aside from DESCRIBE): 2)>
>
> Option 1 is nice and clean, as you would expect from Cake, but it can
> potentially mean a lot of database quering. Option 2 on the other hand
> doesn't look as slick. It puts some logic in my controller I don't
> want there. The related data is split over 2 variables. But the number
> of queries is fixed.
>
> I'm just curious if this issue also bothers other developers and what
> direction do they choose.

I'd go with the latter - 2 explicit and simple queries (although I
think your 1 + number of comments is dependent on the version of cake
you're testing with - should be 2 queries anyway).
You could create a model method to encapsulate your two finds (and any
other logic therin). and even re arrange the resultant array data such
that it's the same as calling find with containable.

The Linkable (or my own OneQuery) behavior is worth using - although
in the use case you're presented it's not relevant.

AD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: