Random stuff and high class geekery by a web developer studio edlington
30 Jan

Prado criteria addOr() behaving strangely. Is this really unpredictable? Propel PHP

No, it’s not.

But maybe you won’t find the solution for hours if I explain the details… :)

In some situations (Propel 1.4), Criteria::addOr() translates to SQL as an AND. The usual example of using:

 
$c = new Criteria(); 
$c->add(BookPeer::TITLE, '%developer%', Criteria::LIKE); 
$c->addOr(BookPeer::TITLE, '%Web%', Criteria::LIKE); 

It translates in SQL as

 
WHERE (book.TITLE LIKE '%developer%' OR book.TITLE LIKE '%Web%')

But addOr() fails on a column with no existing condition, for example:

 
$c = new Criteria();
$c->add(BookPeer::TITLE, '%developer%', Criteria::LIKE);
$c->addOr(BookPeer::CATEGORY, 'IT', Criteria::EQUAL);

It translates in SQL as

 
WHERE book.TITLE LIKE '%developer%' AND book.CATEGORY = 'IT'

This bug will be fixed in Propel 1.5.
Until then, you must use the supported API (i.e. getNewCriterion()) to achieve the desired effect here:

 
$c = new Criteria();
$c1 = $c->getNewCriterion(BookPeer::TITLE, '%developer%', Criteria::LIKE);
$c2 = $c->getNewCriterion(BookPeer::CATEGORY, 'IT', Criteria::EQUAL);
$c1->addOr($c2);
$c->add($c1);
Comments
blog comments powered by Disqus
1/1