簡體   English   中英

Doctrine 多個 where 條件

[英]Doctrine multiple where condition

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

首先要改變你的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')
<?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)

就像@Rikesh說的那樣使用IN表達式,但這是另一種連接AND表達式的好方法

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

產量

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM