2

So this is how I use criteria:

$criteria = Criteria::create();
$criteria->andWhere($criteria->expr()->eq('author', /**/));
$criteria->orWhere($criteria->expr()->eq('author', /**/));
$criteria->orWhere($criteria->expr()->eq('author', /**/));

which results in this sql command:

WHERE 

      (
        (
          t0.author_id = ? 
          OR t0.author_id = ?
        ) 
        OR t0.author_id = ?
      ) 

What if I need this?

WHERE 

      (
        (
          t0.author_id = ? 
          OR t0.author_id = ?
          OR t0.author_id = ?
        )
      ) 

Is there any way how I can change the association of brackets?

Pastx
  • 687
  • 3
  • 8
  • 23

1 Answers1

2

Try this:

$criteria = Criteria::create();
$criteria->andWhere(
    $criteria->expr()->orX(
        $criteria->expr()->eq('author', /**/),
        $criteria->expr()->eq('author', /**/),
        $criteria->expr()->eq('author', /**/)
    )
);
loicfavory
  • 837
  • 6
  • 14
  • Thank you. Is there any way how can i find whether criteria found anything ? As far as i know, they simply dont filter anything and acts like as they dont work if they did not found match. – Pastx Jan 02 '15 at 19:46
  • I'm not an expert with Criteria, but in a query, I think I would do it using a left join, testing if the result is null... – loicfavory Jan 02 '15 at 19:49
  • I have little problem. I need to generate internal expressions like this: foreach($filter->getFilterAuthors() as $author) { $criteria->expr()->eq('author', $author); } Which doesnt work with orX() function that needs parameters. – Pastx Jan 02 '15 at 20:10
  • Take a look at this answer : http://stackoverflow.com/questions/11704447/pass-array-of-conditions-to-doctrine-expr-orx-method. Use the first answer. – loicfavory Jan 03 '15 at 13:48