Introducing raw conditions #182

merged 4 commits into from Nov 7, 2011

2 participants

Propel member

Both Criteria and ModelCriteria can now use raw conditions with a specific binding type. This is indeed a bugfix for the incomplete support of withColumns in ModelCriteria::having().

Example usage can be seen in the added unit tests (to be documented):

// using a raw condition on where()
$books = BookQuery::create('b')
  ->where('LOCATE(\'foo\', b.Title) = ?', true, PDO::PARAM_BOOL)
// SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID 
// FROM `book` 
// WHERE LOCATE('foo', book.TITLE) = true

// using raw condition on having()
$books = BookQuery::create()
  ->withColumn('SUBSTRING(Book.Title, 1, 4)', 'title_start');
  ->having('title_start = ?', 'foo', PDO::PARAM_STR)
// SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID,
//               SUBSTRING(book.TITLE, 1, 4) AS title_start 
// FROM `book` 
// HAVING title_start = 'foo'

The patch would gain to decouple more the raw logic from the model logic, but that would require a deeper refactoring, and I'm not sure we want that in 1.x.

fzaninotto added some commits Nov 7, 2011
@fzaninotto fzaninotto Introducing Criteria::RAW operator (WIP)
This new operators allows a PDO-like binding, specifying a clause and a
binding type. For instance:

$c->add('foo = ?', 123, Criteria::RAW, PDO::PARAM_STR);

This is especially necessary for `having()` clauses, because the
conditions are often based on calculated columns, for which Propel
cannot guess the binding type.
@fzaninotto fzaninotto Add the ability to create a raw Criterion to all Criteria methods 1191479
@fzaninotto fzaninotto Remove just added methods.
This shouls keep the Criteria API to a fairly limited number of methods, although it now behaves a little more magically than before. But doing things the proper way requires a more important refactoring, which can happen in Propel2 but not anymore in the 1.x branch
@fzaninotto fzaninotto Put `ModelCriteria` on par with `Criteria` for raw conditions.
`ModelCriteria::where()` and `ModelCriteria::having()` now accept a
type argument that will force the binding type.
@willdurand willdurand merged commit 03eb6fe into propelorm:master Nov 7, 2011
Propel member

Awesome, thanks for that!

@richmulhern richmulhern pushed a commit to AriaSystems/Propel that referenced this pull request Jun 4, 2014
@themouette themouette add raw sql capability to conditions
see propelorm#182 for more information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment