Snippets

Create an account or login to be able to add, comment and rate snippets.

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.

You need to create an account or log in to post a comment or rate this snippet.