CakePHP 和 HABTM 标签过滤

发布于 2024-08-22 07:16:23 字数 2073 浏览 2 评论 0原文

我正在努力处理 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 FROM news_items AS NewsItem LEFT JOIN users AS User ON ( NewsItem.user_id = User.id) LEFT JOIN camps_news_items AS CampsNewsItem ON (CampsNewsItem.news_item_id = NewsItem.id) WHERE CampsNewsItem code>.camp_id IN (1, 5) ORDER BY NewsItem.已修改 desc LIMIT 10

选择营地id营地名称营地。<代码>创建营地修改CampsNewsItemcamp_idCampsNewsItem code>.news_item_id FROM camps AS Camp JOIN camps_news_items AS CampsNewsItem ON (<代码>CampsNewsItem.news_item_id IN (6,6,7,8) AND CampsNewsItem.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 FROM news_items AS NewsItem LEFT JOIN users AS User ON (NewsItem.user_id = User.id) LEFT JOIN camps_news_items AS CampsNewsItem ON (CampsNewsItem.news_item_id = NewsItem.id) WHERE CampsNewsItem.camp_id IN (1, 5) ORDER BY NewsItem.modified desc LIMIT 10

SELECT Camp.id, Camp.name, Camp.created, Camp.modified, CampsNewsItem.camp_id, CampsNewsItem.news_item_id FROM camps AS Camp JOIN camps_news_items AS CampsNewsItem ON (CampsNewsItem.news_item_id IN (6, 6, 7, 8) AND CampsNewsItem.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-08-29 07:16:23

试试这个:

$params['group'] = array('NewsItem.id');

Try this:

$params['group'] = array('NewsItem.id');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文