学说多重条件

时间:2013-06-04 06:04:28

标签: php zend-framework doctrine-orm

我正在使用Doctrine 2.3我面临着为以下场景设计查询的困难。

SELECT * FROM source WHERE source_id ='10' or source_id ='100' or source_id ='30'

我为单身ID选择做了这个,但我不知道该怎么做。

$qry = $this->manager()->create()
        ->select('e')
        ->from($this->entity, 'e')
        ->where('e.id = :id');

有人可以帮助我吗? 如果我知道上述查询的工作,我将解决我的其他问题.. 跟随。

 SELECT * FROM source WHERE source_id ='10' and source_name ='test' and source_val ='30'

3 个答案:

答案 0 :(得分:11)

首先改变你的where子句,如

->where('e.id IN (:ids)') 
->setParameter('ids', $ids)

$ids = array('10','100','');

要使用和条件进行第二次查询,它应该是类似的,

$qry = $this->manager()->create()
       ->select('e')
       ->from($this->entity, 'e')
       ->where('e.source_id = :id')
       ->andWhere('source_name=?', 'test')
       ->andWhere('source_val=?', '30')

答案 1 :(得分:3)

<?php     
$qry = $this->manager()->create()
    ->select('e')
    ->from($this->entity, 'e')
    ->where('e.id = ?', $eid)
    ->addWhere('source_id = ?', $source_id)
    ->addWhere('field = ?', $value)
    ->addWhereIn('id', array(1,2,3))
    ->addWhere('id = ? AND name = ?', array($id, $name));
?>

<强>输出

SELECT e FROM TblName WHERE e.id = $ eid AND source_id = $ source_id AND field = $ value AND id IN(1,2,3)AND(id = $ id AND name = $ Name)

答案 2 :(得分:1)

就像@Rikesh说使用IN表达式,但这是连接AND表达式的另一种好方法

->where('e.foo = :foo', 'e.bar = :bar')
->setParameters([
   'foo' => $foo,
   'bar' => $bar,
])

<强>输出

... WHERE (e.foo = "foo" AND e.bar = "bar") ...