Also worth noting: A controller can have multiple Models.

Which model would $this->find() use?

On Sep 23, 12:28 pm, "dr. Hannibal Lecter" <[EMAIL PROTECTED]>
wrote:
> Well, calling find() in a controller is not a violation, but not "the
> best practice" if overused.
>
> Of course, calling find() from a controller is the fastest way to get
> your data, but usually, the find() call has other stuff around it,
> stuff that might be common to each call. The idea is to make "fat
> models, thin controllers", meaning that you put as much as you can in
> your models.
>
> Maybe an example is better. Let's say you have a certain criteria for
> getting an article from the Article model. You can call this from a
> controller each time you want to get an article:
>
> $article = $this->Article->find
>         (
>                 'first',
>                 array
>                 (
>                         'conditions' => array('Article.slug' => $slug),
>                         'contain' => array('ArticleCategory', 'Rating')
>                 )
>         );
>
> or you can make it cleaner and easier by declaring a function in your
> model, for example "getArticle":
>
> function getArticle($slug)
> {
>         $_slug = Sanitize::escape($slug);
>
>         return $this->find
>                 (
>                         'first',
>                         array
>                         (
>                                 'conditions' => array('Article.slug' => 
> $_slug),
>                                 'contain' => array('ArticleCategory', 
> 'Rating')
>                         )
>                 );
>
> }
>
> this makes your controller much thinner and easier to follow:
>
> $article = $this->Article->getSingle($slug);
>
> All you have to remember is that all apps work with some sort of data,
> and in Cake, models work with data, not controllers.
>
> I hope that helps a bit. ;)
>
> On Sep 23, 11:33 am, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > Hey guys,
> >  Considering the fact that CakePhp follows the MVC architecture, id
> > like to know...y we dont make function calls such as $this->find("where 
> > id=xyz") from function blocks typed out in Models rather
>
> > than controllers....im sure there is a logical reason to it...but
> > someone asked me this question and i could not give her a good enuf
> > answer....does making function calls to queries like find(), save()
> > work better in models or controllers?.....and is it good practice to
> > pass conditions in your function call while using it in a controller?
> > eg find("where id=1") ;
>
> > Does using $this->find() in controller violate the MVC architecture in
> > any manner?
>
> > Can someone please clarify these question for us?
>
> > Thanks in advance,
> > forrestgump
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to