admin

教义如何在其中使用另一个sql查询编写WhereIn()

sql

我在SQL中有以下查询

... where group_id IN (select group_id from alert where monitor_id = 4);

我想用Doctrine编写它,但是我不知道如何将IN select添加到WHEREIN()子句中!任何的想法 ?

这就是我所做的

$q = $this->createQuery('u') 
    ->select('u.email_address') 
    ->distinct(true)
    // ->from('sf_guard_user u') 
    ->innerJoin('u.sfGuardUserGroup ug') 
    ->where('ug.group_id IN(select group_id from alert where monitor_id=?',$monitor);

$q->execute();

在sfGuardUserTable.class中:

public function getMailsByMonitor($monitor) {


        $q = Doctrine_Query::create()->from("alert a")->where("a.monitor_id", $monitor);
        $groups_raw = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
        $groups = array();
        print_r($groups_raw);
        foreach ($groups_raw as $gr) {
            $groups[] = $gr->id; //line 33
        }


        $q2 = $this->createQuery('u')
                ->select('u.email_address')
                ->distinct(true)
                ->innerJoin('u.sfGuardUserGroup ug')
                ->whereIn("ug.group_id", $groups);
        return $q2->execute();
    }

阅读 182

收藏
2021-07-01

共1个答案

admin

通常,您会执行以下操作:

$q = Doctrine_Query::create()
  ->from('User u')
  ->whereIn('u.id', array(1, 2, 3));

但我认为这更适合您的需求:

$q = Doctrine_Query::create()
  ->from('Foo f')
  ->where('f.group_id IN (SELECT f.group_id FROM Alert a WHERE a.monitor_id = ?)', 4);
2021-07-01