CakePHP查找没有HABTM关系记录的项目

CakePHP查找没有HABTM关系记录的项目,php,cakephp,find,cakephp-2.0,has-and-belongs-to-many,Php,Cakephp,Find,Cakephp 2.0,Has And Belongs To Many,简而言之:Tags HABTM文档 有没有办法找到所有与文档没有关联的标记 我从以下几点开始: $freeTags = $this->Tag->find('all', array( 'conditions' => array( ), 'contain' => array( 'Document' ), 'recursive'

简而言之:
Tags HABTM文档

有没有办法找到所有与
文档
没有关联的
标记

我从以下几点开始:

$freeTags = $this->Tag->find('all', array(
            'conditions' => array(
            ),
            'contain' => array(
                'Document'
            ),
            'recursive' => -1
        ))
但我不知道如何获得如下查询:

SELECT * FROM tags WHERE id NOT IN (SELECT tag_id FROM documents_tags)
我的CakePHP版本是2.3

编辑: 最后的解决方案是
Tag
模型中的一种方法

    $db = $this->getDataSource();
    $subQuery = $db->buildStatement(
            array(
                'fields' => array('DocumentsTag.tag_id'),
                'table' => $db->fullTableName($this->DocumentsTag),
                'alias' => 'DocumentsTag',
                'limit' => null,
                'offset' => null,
                'joins' => array(),
                'conditions' => null,
                'order' => null,
                'group' => null
            ), $this->DocumentsTag
    );
    $subQuery = ' Tag.id NOT IN (' . $subQuery . ') ';
    $subQueryExpression = $db->expression($subQuery);

    $conditions[] = $subQueryExpression;

    $freeTags = $this->find('all', compact('conditions'));
尽你所能

$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
    array(
        'fields'     => array('"DocumentsTag"."tag_id"'),
        'table'      => $db->fullTableName($this->DocumentsTag),
        'alias'      => 'DocumentsTag',
        'limit'      => null,
        'offset'     => null,
        'joins'      => array(),
        'conditions' => null,
        'order'      => null,
        'group'      => null
    ),
    $this->DocumentsTag
);
$subQuery = ' "Tag"."id" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);

$conditions[] = $subQueryExpression;

$this->User->find('all', compact('conditions'));
我希望它能对您有所帮助

尝试一下: