I decided to split the loading of Topic and Posts in to 2 separate calls.
It will fix this pagination issue and allow me to do selective individual
caching on posts as needed.
For anyone who has a similar issue, my new code:
Topics Controller:
public $paginate = array(
'forTopic',
'paramType' => 'querystring',
'limit' => 5,
);
public function view($id)
{
$topic = $this->Topic->find('one', array('conditions' => array('Topic.id'
=> $id)));
$posts = $this->paginate($this->Topic->Post, array('Post.topic_id' => $id));
...
}
Topic Model:
protected function _findOne($state, $query, $results = array())
{
if ($state == 'before')
{
$query['contain'] = array(
'Category',
'Member' => array(
'fields' => array('id', 'name'),
),
);
return $query;
}
return $results[0];
}
And finally, a new finder in the Post Model:
protected function _findForTopic($state, $query, $results = array())
{
if ($state == 'before')
{
$query['contain'] = array(
'Member' => array(
'fields' => array('id', 'name'),
),
'PostContent',
);
return $query;
}
return $results;
}
Thanks,
Alan.
On Thursday, March 15, 2012 8:06:14 PM UTC, Alan Wagstaff wrote:
>
> Hi,
>
> I have a Topic model, which in turn $hasMany Post. In my TopicsController,
> I am doing a find() on the Topic ID to retrieve a single topic and then
> using Containable to get all the posts for the topic. I would like to use
> the Paginate component to limit the number of posts returned when fetching
> the topic but can't figure out how. Everything I've read is about limiting
> the number of topics, rather than posts.
>
> My custom find method in my Topic model:
>
> protected function _findOne($state, $query, $results = array())
> {
>
> if ($state == 'before')
> {
> $query['contain'] = array(
> 'Category',
> 'Member' => array(
> 'fields' => array('id', 'name'),
> ),
> 'Post' => array(
> 'Member' => array(
> 'fields' => array('id', 'name'),
> ),
> 'PostContent',
> ),
> );
>
> return $query;
> }
>
> return $results;
>
> }
>
> This gives me a resultset in the following format:
>
> array(
> 0 => array(
> 'Topic' => array(
> ...
> ),
> 'Category' => array(
> ...
> ),
> 'Member' => array(
> ...
> ),
> 'Post' => array(
> 0 => array(
> 'Member' => array(
> ...
> ),
> 'PostContent' => array(
> 0 => array(
> ...
> )
> )
> ),
> 1 => array(
> ...
> ),
> ... etc ...
> )
> )
> )
>
> My TopicsController currently has the following (although it has no
> effect):
>
> public $paginate = array(
> 'one',
> 'paramType' => 'querystring',
> 'Post' => array(
> 'limit' => 5,
> )
> );
>
> public function view($id)
> {
>
> $topic = $this->paginate(array('Topic.id' => $id));
> ...
> }
>
> So, to clarify, I want to fetch a single topic, but use Paginate to limit
> the number of posts that are returned and allow me to use pagination links
> in the view.
>
> Thanks in advance,
> Alan.
>
>
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php