Home » Php » php – Doctrine multiple where condition

php – Doctrine multiple where condition

Posted by: admin July 12, 2020 Leave a comment

Questions:

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'
How to&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')

Answer:

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

Answer:

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") ...