Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine multiple where condition

I am Working with Doctrine 2.3 I am facing difficulty to design a Query for the below scenario.

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

I did this for single Id selection but I am not sure how to do this.

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

Can some one help me in this? If I get to know the working for the above query I will solve my other issues.. As Follows.

 SELECT * FROM source WHERE source_id ='10' and source_name ='test' and source_val ='30'
like image 493
DonOfDen Avatar asked Jun 04 '13 06:06

DonOfDen


3 Answers

For first one change your where clause like,

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

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

And to use and condition for your second query it should be something like,

$qry = $this->manager()->create()
       ->select('e')
       ->from($this->entity, 'e')
       ->where('e.source_id = :id')
       ->andWhere('source_name=?', 'test')
       ->andWhere('source_val=?', '30')
like image 85
Rikesh Avatar answered Oct 30 '22 23:10

Rikesh


<?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));
?>

OutPut

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)

like image 28
Ahmed Abumostafa Avatar answered Oct 30 '22 22:10

Ahmed Abumostafa


Like @Rikesh says use IN expression but here is another nice way to concatenate AND expressions

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

OutPut

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

like image 1
Sfblaauw Avatar answered Oct 30 '22 21:10

Sfblaauw