代码之家  ›  专栏  ›  技术社区  ›  anderstornvig

如何获取所有没有在CakePHP中关联POST的标记

  •  1
  • anderstornvig  · 技术社区  · 15 年前

    我正在用CakePHP写博客,所以我的数据库中有两个与HABTM相关的表:posts和tags。因为它们是HABTM相关的,所以我也有一个跟踪关系的后期表格。

    我希望在我的tags\u控制器中有一个方法可以删除所有未使用的标记。

    2 回复  |  直到 15 年前
        1
  •  2
  •   dhofstet    15 年前

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

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

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

        2
  •  0
  •   kiang    15 年前
    $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',
            ),
        ),
    ),
    

    ));