Reputation: 1171
Wow, CakePHP really hasn't got this problem sorted.
After hours of searching I came across the solution below (which may or may not be outdated), but I'm having issues applying paginatior 'limit' => 10 or other ordering.
Any ideas what I'm missing?
My model:
public $hasAndBelongsToMany = array(
'Post' => array(
'className' => 'Post',
'joinTable' => 'tags_posts',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'post_id',
'order' => array('Post.created DESC'),
'unique' => true
)
);
In my controller in view()
public function view($id) {
$this->Tag->bindModel(array('hasOne' => array('TagsPost')), false);
$this->set('tag', $this->paginate('Tag', array('TagsPost.tag_id' => $id)));
}
In my view I then had to change:
foreach ($tag['Post'] as $post)
to
foreach ($tag[0]['Post'] as $post)
Upvotes: 2
Views: 1495
Reputation: 354
Tested with CakePHP 2.x
public function view ($id)
{
$this->paginate = array (
'limit' => 10,
'conditions' => array('TagsPost.tag_id' => $id)
);
$this->Tag->bindModel(array('hasOne' => array('TagsPost')), false);
$this->set('tag', $this->paginate('Tag'));
}
Upvotes: 0
Reputation: 5464
Your view method should looks like:
public function view($id) {
$this->Paginate['limit'] = 10;
$this->Paginate['conditions'] = array('TagsPost.tag_id' => $id);
$this->Tag->bindModel(array('hasOne' => array('TagsPost')), false);
$this->set('tag', $this->paginate('Tag'));
}
Kindly ask if it not worked for you.
Upvotes: 2