user2558528
user2558528

Reputation: 1

Creat not query in CakePhp

How can I do this query in CakePhp?

select id from users where id not in (select userid from user_details ud join users u ON u.id=ud.userid AND ud.name ='fullname');

Upvotes: 0

Views: 55

Answers (2)

arilia
arilia

Reputation: 9398

to obtain your exact query you have to do:

$this->User->find(
    'all',
    array(
        'fields' => array('User.id'),
        'conditions' => array('id NOT IN (select userid from user_details ud join users u ON u.id=ud.userid AND ud.name ='fullname')' 
    );

but you can simply achieve the same result doing

$this->User->find(
    'all',
    array(
        'fields' => array('User.id'),
        'conditions' => array('UserDetail.name !=' => 'fullname'),
        'contain' => array('UserDetail')
    );

assuming your User model is in realtionship (hasOne?) with UserDetail

Upvotes: 2

Arka
Arka

Reputation: 591

Easiest way. but not a native cakephp, always try to do with find() method.

$this->User->Query("select id from users where id not in (select userid from user_details ud join users u ON u.id=ud.userid AND ud.name ='fullname')");

Upvotes: 2

Related Questions