Ahh haa!
After much staring into the deepest, darkest bowels of the paginate
functionality I finally worked out - conditions are not part of the
object - they are part of the scope of pagination. So the magic below
works:
function index()
{
// Overide paginate limit, may need to change
$this->cacheAction = '1 hour';
$this->passedArgs['limit'] = 1;
$this->set('posts', $this->paginate(null,
array('Post.published'=>1)));
}
On 5/16/07, Tane Piper <[EMAIL PROTECTED]> wrote:
> Hey folks,
>
> I've noticed a little bug in my code, overlooked it before but now I
> need to fix it. In my Post controller, I paginate my query - but it
> seems to be ignoring the conditions I pass to it. Basically the
> condition is to check if a post is published. Now, In my default var,
> I don't have the condition, as I want to be able to show non-published
> stories in the admin views. This is what it looks like:
>
> var $paginate = array('fields'=>array('Post.id', 'Post.title',
> 'Post.byline', 'Post.body', 'Post.comments_count' ,'Post.user_id',
> 'Post.url' ,'Post.post_date' , 'Post.published' ,'User.id',
> 'User.username'),'limit'=>10,
> 'order'=>array('Post.post_date'=>'DESC'));
>
> However, in my index method, I change the limit to 1 story, and try to
> pass in the condition to only show published posts:
>
> function index()
> {
> $this->cacheAction = '1 hour';
> $this->passedArgs['limit'] = 1;
> $this->passedArgs['conditions'] = 'Post.published = 1';
> $posts = $this->paginate();
> $this->set(compact('posts'));
> }
>
> Now the limit value works fine, it only shows 1 post at a time. But
> the conditions seems to be ignored. However, I have another method
> that uses findAll, and when I pass in the condition it works fine:
>
> function getposts($limit = 5)
> {
> $getposts = $this->Post->findAll('Post.published = 1',
> array('Post.id','Post.url','Post.title',
> 'Post.byline','User.username'), 'Post.post_date DESC', $limit);
> if(isset($this->params['requested'])) {
> return $getposts;
> }
> $this->set('getposts', $getposts);
> }
>
> Can anyone explain to me why it's not working? I've looked through
> the API for paginate and as far as I could see, that should be fine?
>
> Thanks,
> Tane
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---