您使用find()函数的方式意味着您需要所有行标记表。
在你的模型:
class Post extends AppModel {
/**
* @see Model::$actsAs
*/
public $actsAs = array(
'Containable',
);
public $hasAndBelongsToMany = array('Tag');
}
class Tag extends AppModel {
/**
* @see Model::$actsAs
*/
public $actsAs = array(
'Containable',
);
public $hasAndBelongsToMany = array('Post');
}
您应该使用查找功能是这样的:
$postWithTag = $this->Post->find('first', array('conditions' => array('Post.id' => $post_id),'contain'=>array('Tag')));
返回后与它的标签。
,如果你只希望不交,你应该把属于关联关系的PostTag模型标签:
class PostTag extends AppModel {
/**
* @see Model::$belongsTo
*/
public $belongsTo = array(
'Post','Tag'
);
}
然后使用查找功能是这样的:
class PostController extends AppController {
/**
* @see Controller::$uses
*/
public $uses = array(
'Post', 'PostTag'
);
public function post(){
/**
* Your code
*/
$tags = $this->PostTag->find('all', array('conditions' => array('PostTag.post_id' => $post_id)));
$this->set('tags',$tags);
}