Code snippets for symfony 1.x

Navigation

Custom criteria for comparing 2 fields from the same record

Imagine that you have a table with the following columns:

MyTable
-------
id
col1
col2

If you want to retrieve the records having col1 greater than col2, the following doesn't work:

$c = new Criteria();
$c->add(MyTablePeer::COL1, MyTablePeer::COL2,  Criteria::GREATER_THAN);

Instead, you need to add a custom condition to your Criteria:

$c = new Criteria();
$c->add(MyTablePeer::COL1, MyTablePeer::COL1.'>='.MyTablePeer::COL2, Criteria::CUSTOM);
by Francois Zaninotto on 2006-07-10, tagged criteria  propel 

Comments on this snippet

gravatar icon
#1 brikou on 2006-10-31 at 11:26

great but it seems like the 1st parameter is not important at all, so why not replace it by null or another variable meaning "I'm not important"

{{{ $c->add(MyTablePeer::COL1, MyTablePeer::COL1.'>='.MyTablePeer::COL2, Criteria::CUSTOM); }}}

by

{{{ $c->add(null, MyTablePeer::COL1.'>='.MyTablePeer::COL2, Criteria::CUSTOM); }}}

or

{{{ $c->add(BasePeer::NULL, MyTablePeer::COL1.'>='.MyTablePeer::COL2, Criteria::CUSTOM); }}}

gravatar icon
#2 Michel D'HOOGE on 2006-11-07 at 11:42

The 1st parameter is indeed used by Propel when stripping down the criteria for the count request. In an application, I have the following code, which works correctly. $c->add(MyTablePeer::TAGS, 'MATCH('.MyTablePeer::TAGS .') AGAINST (\''.$tags.'\' IN BOOLEAN MODE)', Criteria::CUSTOM);

As soon as I replace the 1st parameter by something else, Propel fails to create a correct count request.

gravatar icon
#3 stormsson on 2009-09-16 at 10:40

thank you. i was stuck on that. but i don't understand why that couldn't work...

gravatar icon
#4 Barclay Dunn on 2009-12-14 at 07:31

Does anyone know why it is that

$c->add(MyTablePeer::COL1, MyTablePeer::COL2, Criteria::GREATER_THAN);

doesn't work? It seems like a really obvious thing for people to want to do. Having to create a custom Criteria in this case seems like a hack. (No disrespect intended.)