7

我正在使用 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'
4

3 回答 3

13

对于第一个更改您的 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')
于 2013-06-04T06:10:00.777 回答
2
<?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)

于 2013-06-04T07:28:26.147 回答
1

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

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

输出

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

于 2018-02-12T17:09:37.337 回答