Tuesday, April 26, 2011

Re: read(null,$id) or findById($id) and limit data in associated model

On Tue, Apr 26, 2011 at 4:35 PM, Salines <nikola.paradzik@gmail.com> wrote:
> Sorry my English is not good.
>
> when I call exp, www.example.com/brands/view/2
>
> Show some information about the brand and the last 9 related products
> added to the database, which are active 1 (online status).
>
> The brand has many products, maybe 100, but I need only last 9.

The default 'recursive' option should cause Cake to grab the
associated Products if your model associations are set up correctly.

That said, to include just the 9 most recent Products, you should
consider using ContainableBehavior.

$this->set(
'data', // not $this->data
$this->Brand->fetchWithProducts($id)
);

Brand model:

public function fetchWithProducts($id)
{
return $this->find(
'first',
array(
'conditions' => array(
'Brand.id' => $id
),
'contain' => array(
'Product' => array(
'order' => array(
'Product.created' => 'DESC'
),
'limit' => 9
)
)
)
);
}

view:
debug($data);

--
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: