lox / pheasant

A lightweight data mapper designed to take advantage of PHP 5.3+
http://getpheasant.com
MIT License
101 stars 21 forks source link

Chaining and() and or() in Criteria now works #29

Closed lox closed 11 years ago

lox commented 11 years ago

This is a response to bug #27.

As originally designed, the and() and or() methods on a Criteria object didn't chain:

<?php

$c = new Criteria();
$c->and('llamas = true')->and('quantity > 1');
// results in (quantity > 1)

Obviously this wasn't expected, or the desired behavior, but it came about to support complex chaining like:

<?php

$c = new Criteria();
$c->or(
   $c->and('llamas = true', 'quantity > 1'),
   'alpaca = true'
);
// results in (llamas = true AND quantity > 1) OR alpaca = true

The more I've thought about that syntax, the crazier it is. The above is now accomplished like this:

<?php

$c = new Criteria();
$c->or(
   Criteria::concatAnd('llamas = true', 'quantity > 1'),
   'alpaca = true'
);
// results in (llamas = true AND quantity > 1) OR alpaca = true

Chaining now works as you'd expect it to:

<?php

$c = new Criteria();
$c->and('llamas = true')->and('quantity > 1');
// results in ((llamas = true) AND quantity > 1)