There are usually lots of Has And Belongs To Many relationship questions on the CakePHP mailing list. Since I am stupid about this stuff, I sought out Nate Abele and bugged him via IM until he agreed to give me an example of how to do this. Thanks Nate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Tag extends AppModel {
var $hasAndBelongsToMany = 'Post';
function paginate($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
$tag = $conditions['tag'];
unset($conditions['tag']);
$this->hasAndBelongsToMany['Post'] = am(
$this->hasAndBelongsToMany['Post'],
compact('conditions', 'fields', 'order', 'limit', 'page')
);
return $this->findByName($tag);
}
function paginateCount($conditions = null) {
$tag = $conditions['tag'];
unset($conditions['tag']);
$tmp = $this->hasAndBelongsToMany['Post'];
$this->hasAndBelongsToMany['Post']['fields'] = array('id');
$tag = $this->findByName($tag);
$this->hasAndBelongsToMany['Post'] = $tmp;
return count($tag['Post']);
}
}
?>
<?php
// Controller code
$data = $this->paginate('Tag', array('tag' => $tag));
// Where $tag = some tag name