CakePHP 和 HABTM 标签过滤
我正在努力处理 CakePHP HABTM 关系,并且在尝试完成一项看似简单的任务时陷入困境。我有很多营地和很多新闻项目。每个 NewsItem 可能与任何营地相关,因此当用户创建 NewsItem 时,他们会检查该项目适用于哪些营地。就是这个想法。
所以...
我想将一些阵营过滤合并到我的 NewsItems 视图中。换句话说,我想查看与“A”阵营和“B”阵营相关的所有新闻条目。
向“查找”和“分页”功能添加条件就像一个魅力。
然而...
如果一个NewsItem属于多个Camp,它会在列表中出现多次。因此,当我告诉 find 给我属于阵营“A”和阵营“B”的所有 NewsItem 时,属于阵营“A”和阵营“B”的 NewsItem 将出现两次。
调试查询如下所示:
选择
NewsItem
.id
,NewsItem
.user_id
,NewsItem
.<代码>标题,NewsItem
。正文
,NewsItem
。修改
,用户.
first_name
FROMnews_items
ASNewsItem
LEFT JOINusers
ASUser
ON (NewsItem
.user_id
=User
.id
) LEFT JOINcamps_news_items
ASCampsNewsItem
ON (CampsNewsItem
.news_item_id
=NewsItem
.id
) WHERECampsNewsItem
code>.camp_id
IN (1, 5) ORDER BYNewsItem
.已修改
desc LIMIT 10
选择
,营地
。id
,营地
。名称
,营地
。<代码>创建营地
。修改
,CampsNewsItem
。camp_id
,CampsNewsItem
code>.news_item_id
FROMcamps
ASCamp
JOINcamps_news_items
ASCampsNewsItem
ON (<代码>CampsNewsItem.news_item_id
IN (6,6,7,8) ANDCampsNewsItem
.camp_id
=Camp< /code>.
id
) WHERE 1 = 1
php 代码如下所示:
$camp_ids = array(1, 3, 6, 10);
$conditions = array('CampsNewsItem.camp_id' => $camp_ids);
$params['conditions'] = $conditions;
$this->NewsItem->bindModel(array('hasOne' => array('CampsNewsItem')));
$news_items = $this->NewsItem->find('all', $params);
感谢您的见解!
I am wrestling around a bit with CakePHP HABTM relationships, and I've gotten stuck trying to do a seemingly simple task. I have many Camps and many NewsItems. Each NewsItem could be relevant to any of the camps, so when a user creates a NewsItem, they check which Camps the item is for. That's the idea.
So...
I would like to incorporate some camp filtering into my NewsItems views. In other words, I would like to see all NewsItems relevant to Camp "A" and Camp "B".
Adding conditions to the "find" and "paginate" functions work like a charm.
However...
If a NewsItem belongs to multiple Camps, it appears multiple times in the list. So when I tell find to give me all NewsItems that belong to Camp "A" and Camp "B", a NewsItem that belongs to Camp "A" and Camp "B" will appear twice.
The debug query looks like this:
SELECT
NewsItem
.id
,NewsItem
.user_id
,NewsItem
.heading
,NewsItem
.body
,NewsItem
.modified
,User
.first_name
FROMnews_items
ASNewsItem
LEFT JOINusers
ASUser
ON (NewsItem
.user_id
=User
.id
) LEFT JOINcamps_news_items
ASCampsNewsItem
ON (CampsNewsItem
.news_item_id
=NewsItem
.id
) WHERECampsNewsItem
.camp_id
IN (1, 5) ORDER BYNewsItem
.modified
desc LIMIT 10SELECT
Camp
.id
,Camp
.name
,Camp
.created
,Camp
.modified
,CampsNewsItem
.camp_id
,CampsNewsItem
.news_item_id
FROMcamps
ASCamp
JOINcamps_news_items
ASCampsNewsItem
ON (CampsNewsItem
.news_item_id
IN (6, 6, 7, 8) ANDCampsNewsItem
.camp_id
=Camp
.id
) WHERE 1 = 1
The php code looks like this:
$camp_ids = array(1, 3, 6, 10);
$conditions = array('CampsNewsItem.camp_id' => $camp_ids);
$params['conditions'] = $conditions;
$this->NewsItem->bindModel(array('hasOne' => array('CampsNewsItem')));
$news_items = $this->NewsItem->find('all', $params);
Thanks for any insight!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: