2

私は CakePHP 2.1 を使用してい
ます。次のモデルと関係があるとしましょう。
PostsEdition
PostsEditors
PostsTags

私は から推論しており、特定の に属するPostsControllerすべてのものを見つけたいと思っています。それは関連しており、関連しています。 PostsEditionEditor.id=55Tag.id=33

PostsHABTM リレーションの条件に基づく検索が、検索の方向と検索の理由をEditor.

$this->Editor->find('all',
    array(
        'conditions'=>array('Editor.id'=>55),
        'contain'=>array('Post')
    )
);

残念ながら、条件を付けたい HABTM リレーションが複数あるため、ここでは機能しません。

またcontain、ブランチ ( ) を切り離すだけで、editors親 ( ) のフィルタリングには役立たないため、使用は機能しませんposts

これを解決するにはどうすればよいですか?
アドホック結合に頼る必要があるのでしょうか? もしそうなら、誰かが私にどのようなアプローチを取るべきかを大まかに説明してもらえますか?

編集: 私が達成したいことを説明するためのいくつかの擬似コード:

$this->Post->find('all',
    array('conditions'=>array(
            'Edition.id'=>4,
             // start pseudo code
            'Post.Editor' => 'has at least one related editor with id=55',
            'Post.Tag' => 'has at least one related tag with id=33',
        )
    )
);

よろしく、バート

ソリューションの編集: @RichardAtHome に従って、以下のソリューションを作成しました。(まだ)複数の結合条件を採用していませんが、それは必要ではないようです:

// Paginate over Cards that belong to current Subscription (belongsTo) AND the User is related to as an Editor (HABTM)
$this->paginate = array(
    'fields' => array('id','title','workingtitle','attachment_count','subscription_id'),
    'joins' => array(
        // create Editor-join
        array(
            'table' => 'cards_users',
            'alias' => 'Editor',
            'type' => 'left',
            'conditions' => array(
                'Editor.card_id = Card.id'
            )
        ), 
        array( 
            'table' => 'users', 
            'alias' => 'User', 
            'type' => 'left',
            'conditions'=> array( 
                'User.id = Editor.user_id'
            ) 
        )
    ),
    'conditions' => array(
        'OR' => array(
            // is Card in current subscription? (straightforward condition)
            array('Card.subscription_id',$subscription_id),
            // is current User assigned as Editor? (condition works with join above)
            array('User.id' => $user['id'])
        )
    ),
    'limit' => 10
);
4

2 に答える 2

6

このタイプのクエリの場合、ケーキは結合せず、代わりに複数のクエリを実行するため、通常は自分で結合を作成するのが最も簡単だと思います (テーブル a から一致する行を選択し、テーブル b から一致する行をフェッチします)。

クエリは次のようにする必要があります。

$this->Post->find('all',
    array(
        'conditions'=>array('Post.edition_id'=>4),
        'joins'=>array(
            array(
                'type'=>'inner',
                'table'=>'posts_editors',
                'alias'=>'PostsEditor',
                'conditions'=>array(
                    'PostsEditor.post_id = Post.id',
                    'PostsEditor.editor_id'=>55 // your criteia for editor id=55
                )
            ),
            array(
                'type'=>'inner',
                'table'='posts_tags',
                'alias'=>'PostsTag',
                'conditions'=>array(
                    'PostsTag.post_id = Post.id',
                    'PostsTag.tag_id'=>33 // your criteria for tag id = 33
                )

            )
        )
    )
);

出力された SQL をチェックして、このクエリが実際に舞台裏で何をしているのかを確認してください。

于 2012-08-23T13:11:58.550 に答える
0

試す:

$this->Post->find('all',
        array('conditions'=>array(
                'Edition.id'=>4,
                'Editor.id' => '55',
                'Tag.id' => '33',
            )
        )
    );
于 2012-08-23T11:36:19.867 に答える