【发布时间】:2013-03-10 07:05:11
【问题描述】:
我正在尝试进行搜索,对具有特定标签或标签的帖子使用分页(例如,如果用户要选择两个标签,则将返回包含任一标签的帖子)。
我在我的帖子表中定义了关系
public $hasAndBelongsToMany = array('Tags' => array(
'className' => 'Tags',
'joinTable' => 'posts_tags',
'foreignKey' => 'post_id',
'associationForeignKey' => 'tag_id',
'unique' => 'keepExisting'));
如何使用 Find 检索具有给定标签的行(名称或 ID 都可以)
尝试:
// other pagination settings goes here
$this->paginate['conditions']['Tags.id'] = 13;
给我一个关系不存在的错误。
查看调试信息,这些表似乎没有加入 Posts_Tags 和 Tags 表,但是,当我将数据调试到视图时,Posts 对象包含标签数据。
我能找到的大部分文档似乎都围绕着早期版本的 CakePHP,我们将不胜感激。
【问题讨论】:
-
将外键名称检查到相关表中。
-
表的架构如下: CREATE TABLE
posts_tags(idint(10) unsigned NOT NULL AUTO_INCREMENT,post_idint(10) unsigned NOT NULL,tag_idint (10)无符号非空,主键(id),键post_tags_post_id_fk(post_id),键post_tags_tag_id_fk(tag_id),约束post_tags_post_id_fk外键(post_id)3345参考@9876 (id) ON DELETE CASCADE ON UPDATE CASCADE, 约束post_tags_tag_id_fkFOREIGN KEY (tag_id) 参考tags(id) ON DELETE CASCADE ON UPDATE CASCADE);
标签: cakephp cakephp-2.0