<roundrightfarm@gmail.com> wrote:
> I'm just learning cake and am trying to better understand the
> relationship between and appropriate use of the $uses variable in a
> controller, the Containable Behavior, and of the different Model
> relationships ($hasOne, $belongsTo, etc.)
>
> It seems they all influence the scope of a Model->find() call. So how
> to do you decide whether you want to use one or the other.
>
> Is it such that the best practice is to always flesh out your Model
> relationships as fully as possible, and then only use $uses when
> necessary to establish a relationship not present in the Models,
>
> OR Should you keep the model relationships as sparse as possible,
> deciding on a case by case basis when to code a Model relationship and
> when to use $uses.
They're different things. The associations are defined within models,
while $uses is a controller class var that determines which model
classes should be loaded. For example, if you have a Post model, it
might be associated with Comment and User. This allows you to fetch
associated data. It also lets you do things like
$this->Comment->find() in the Post model. The $uses var defaults to
the model for the controller, but you can add others. Say you have a
NewsletterController. You might want to include User in $uses so that
you can get a list of recipients. However, this would mean that User
is loaded for every Newsletter action. What I've done instead is use
ClassRegistry::init within the Newsletter model:
public function fetchRecipients()
{
return Set::extract(
ClassRegistry::init('User')->find(
'all',
array(
'conditions' => array('User.email IS NOT NULL'),
'fields' => array('DISTINCT User.email'),
'order' => array('User.email' => 'ASC'),
'recursive' => -1
)
),
'{n}.User.email'
);
}
Doing it this way means that I only load the User class when it's needed.
Maybe a better example of including multiple models in $uses would be
if you had an UploadsController but no Upload model. Instead, you
might have Image, PDF, and Spreadsheet models.
> And as far as the Containable behavior is concerned, is it best
> practice to use it whenever possible to limit the memory usage of a
> Model->find() call, or should you use it only when memory becomes an
> issue.
I think it's a good idea to avoid fetching too much iunnecessary data
in the first place. That's where Containable shines. However,
convenient as it is, it comes with a bit of baggage, in that it can
lead to LOTS of extra queries. While very helpful, it's not smart
enough to use joins in an efficient manner. For some people, it's a
showstopper. Personally, I usually include it in AppModel and use it
for most finds because I also make heavy use of caching, so I'm not
too concerned about the extra queries.
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
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
No comments:
Post a Comment