3

簡単に言えば:Tags HABTM Documents

関連付けられTagsていないものをすべて見つける方法はありますか?Document

私はこれから始めました:

$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'));
4

1 に答える 1

1

クックブックとして::サブクエリを実行できます

$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'));

お役に立てば幸いです

于 2013-02-12T23:58:15.550 に答える