mysql - Query to find where associated model (has many) is empty in cakephp -
i have model named application
. , application
associated has_many
model named location
.
application
has many location
in application query:
$this->application->find('all', array('conditions' => 'application.status' => 'accepted'));
i'm finding applications
status
accepted
.
next thing achieve find application
records associated location
empty/null or in other words count
of location
records 0.
i tried make join
query this:
$join_query = array( 'table' => 'locations', 'alias' => 'location', 'type' => 'inner', 'conditions' => array( 'location.application_id = application.id', 'or' => array( array('location.id' => null) ) ) );
but seems it's querying application
records have associated location
records.
thanks in advanced if guys have idea(s).
you need use left join, not inner join. inner join results have row in both of tables joining, want results there row in left table. left joins results in left table, regardless if there's row associated in right table. add condition after join complete, select joined results location.id null.
$this->application->find('all', array( 'conditions' => array('location.id' => null), 'joins' => array( array( 'table' => 'locations', 'alias' => 'location', 'type' => 'left', 'conditions' => array('location.application_id = application.id') ), ), ) );
Comments
Post a Comment