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.
精彩评论