开发者

CakePHP Pagination problem with binding models

Im using a Model Post. it doesn't have any associations. but for a paginated action i need to display the user name also, so used $this->Pos开发者_运维技巧t->bindModel() to attach User model to the Posts. But i'm not getting the Users attached with the Post in the resulting query. The problem i think is the paginate method initially makes a call like find('count') to get the total no of results, so the bind is removed from there on. Have a look at my paginate variable

var $paginate = array(
        'limit'=>10,
        'order'=>'created DESC'
    );

In the action

$this->Post->bindModel(array(
            'belongsTo'=>array(
                'User'=>array(
                    'className'=>'User',
                    'fields'=>'User.username,User.id',
                    'foreignKey'=>'user_id'
                )
            )
        ));

$this->paginate('Post');


If you use bindModel, after a call to a find method, the model gets unbinded.

You need to add a boolean false, like this: bindModel(array(...),false).

This way it will still be binded after the paginateCount call ...


the fields should be 'fields'=>array(..). But I think you shouldn't specify that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜