Где нет - учение - PullRequest
       13

Где нет - учение

1 голос
/ 04 февраля 2012
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();

Как я могу получить все данные из таблицы SECOND без first_id, существующего в таблице FIRST?

1 Ответ

2 голосов
/ 05 февраля 2012

Подойдите к одному (похоже на ваш пример кода):

// 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();

Другой способ получить тот же результат в одном составном запросе:

$two = Doctrine_Query::create()
  ->from('Second s')
  ->where('first_id NOT IN (SELECT id FROM First)');
return $two->execute();
...