如何获取在 CakePHP 中没有关联帖子的所有标签

发布于 2024-08-05 17:26:57 字数 185 浏览 1 评论 0原文

我正在 CakePHP 中写博客,因此我的数据库中有两个与 HABTM 相关的表:帖子和标签。因为它们与 HABTM 相关,所以我还有一个 posttags 表来跟踪关系。

我想在我的tags_controller 中有一个方法来删除所有未使用的标签。

如何查找与任何帖子无关的所有标签?

I'm doing a blog in CakePHP, so I have two tables in my database that are HABTM related: posts and tags. Because they are HABTM related I also have a poststags table keeping track of relations.

I'd like to have a method in my tags_controller that will delete all unused tags.

How do I find all tags that are NOT associated with any posts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

森林散布 2024-08-12 17:26:57

您可以使用以下语句删除所有未使用的标签:

$this->query('delete from Tags where not isn't (select * from posts_tags where posts_tags.tag_id = Tags.id)');

(要查找与任何帖子无关的所有标签,只需将“删除”替换为“选择 *”)

You could use the following statement to delete all unused tags:

$this->query('delete from tags where not exists (select * from posts_tags where posts_tags.tag_id = tags.id)');

(And to find all tags not associated with any posts simply replace the "delete" with "select *")

把梦留给海 2024-08-12 17:26:57
$this->Tag->find('all', array(
'conditions' => array(
    'PostsTag.id IS NULL'
),
'joins' => array(
    array(
        'table' => 'poststags',
        'alias' => 'PostsTag',
        'type' => 'LEFT',
        'conditions' => array(
            'PostsTag.tag_id' => 'Tag.id',
        ),
    ),
),

));

$this->Tag->find('all', array(
'conditions' => array(
    'PostsTag.id IS NULL'
),
'joins' => array(
    array(
        'table' => 'poststags',
        'alias' => 'PostsTag',
        'type' => 'LEFT',
        'conditions' => array(
            'PostsTag.tag_id' => 'Tag.id',
        ),
    ),
),

));

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