Symfony: How to query using Criteria OR


I’m looking for way to do a simple query using criteria OR in symfony framework. The official documentation does not covers the sql query criteria for OR statement. After googling for while, there is some reference from the Propel Guide

To perform a simple SQL query with OR criteria is a bit complicated in symfony or i can say in Propel. Here’s the example of how to perform the SQL query for:

Advertisements


SELECT * FROM table_a WHERE (column_a = 1 OR column_a = 3) AND column_c = 'xxx'

$c = new Criteria;
$c1 = $c->getNewCriterion(TableAPeer::COLUMN_A, 1);
$c2 = $c->getNewCriterion(TableAPeer::COLUMN_A, 2);
$c1->addOr($c2);
$c->add(TableAPeer::COLUMN_B, ‘xxx’);
$c->add($c1);

So, now you can perform your query using criteria OR in symfony.
Yes, no doubt it’s complicated but it works!




Share this with your friends:-

2 Responses to “Symfony: How to query using Criteria OR”

  1. rizwan says:

    straight from the propel guide, to perform simple or comperrison

    // Find all authors with last name of Tolstoy, Dostoevsky, or Bakhtin
    $c2 = new Criteria();
    $c2->add(AuthorPeer::LAST_NAME, array(“Tolstoy”, “Dostoevsky”, “Bakhtin”), Criteria::IN);

  2. Ritesh M. Tamrakar says:

    Thank you very much for giving clue for how to add more criteria with or and and. 🙂
    It is very different than i thought. SQL people needs to bang owns head many time on wall to figure out how to use ORM based query.

Leave a Reply