FIRST:
id | name
1 | sss
2 | ddd
5 | fff
$q = Doctrine_Query::create()
->from('First a');
return $q->execute();
SECOND:
id | first_id | name
....
$two = Doctrine_Query::create()
->from('Second a')
->whereInNot(first_id, $q) // ???????
;
return $two->execute();
How can i get all data from table SECOND without first_id existing in table FIRST?
Approach one (similar to your example code):
// Retrieve ONLY the id, and use hydration mode that produces a simple array of results.
$first_ids = Doctrine_Query::create()
->select('f.id')
->from('First f')
->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
$two = Doctrine_Query::create()
->from('Second s')
->whereNotIn('first_id', $first_ids);
return $two->execute();
Another approach to get the same result in a single compound query:
$two = Doctrine_Query::create()
->from('Second s')
->where('first_id NOT IN (SELECT id FROM First)');
return $two->execute();