doctrine如何用其他sql查询写入WhereIn() [英] doctrine how to write WhereIn() with another sql query inside

查看:284
本文介绍了doctrine如何用其他sql查询写入WhereIn()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL中有如下的查询

  ...其中group_id IN(从alert中选择group_id,其中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')
- >其中('ug.group_id IN(从alert中选择group_id from 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; //行33
}


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


解决方案

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

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


$ b $ ('Foo f')
- >其中('f.group_id IN('f $ f $') (SELECT f.group_id FROM Alert a WHERE a.monitor_id =?)',4);


i have the folowing query in SQL

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

I want to write it in Doctrine but i don't know how to add the IN select into WHEREIN() clause ! any idea ?

this is what i did

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

In the 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();
    }

解决方案

Usually you would do something like:

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

But I think this one better fits your needs:

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

这篇关于doctrine如何用其他sql查询写入WhereIn()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆