Skip to content

Commit

Permalink
Merge branch 'release/1.1.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
romaninsh committed Mar 10, 2017
2 parents a64210f + 437526a commit 0a924af
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.1.11

Added support for OR conditions.

http://agile-data.readthedocs.io/en/develop/conditions.html#adding-or-conditions

## 1.1.10

Aggregate fields (hasMany->addField) will now coalesce results
Expand Down
26 changes: 26 additions & 0 deletions docs/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ Normally, however, you would use a different fields::

You can have as many conditions as you like.

Adding OR Conditions
--------------------

In Agile Data all conditions are additive. This is done for security - no matter what
condition you are adding, it will not allow you to circumvent previously added condition.

You can, however, add condition that contains multiple clauses joined with OR operator::

$m->addCondition([
['name', 'John'],
['surname', 'Smith']
]);

This will add condition that will match against records with either name=John OR surname=Smith.
If you are building multiple conditions against the same field, you can use this format::

$m->addCondition('name', ['John', 'Joe']);

For all other cases you can implement them with :php:meth:`Model::expr`::

$m->addCondition($m->expr("(day([birth_date]) = day([registration_date]) or day([birth_date]) = [])", 10));

This rather unusual condition will show user records who have registered on same date when they were born OR if
they were born on 10th. (This is really silly condition, please don't judge, if you have a better example, I'd love
to hear).

Defining your classes
---------------------

Expand Down
29 changes: 26 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,34 @@ public function offsetUnset($name)
public function addCondition($field, $operator = null, $value = null)
{
if (is_array($field)) {
array_map(function ($a) {
call_user_func_array([$this, 'addCondition'], $a);
}, $field);
$this->conditions[] = [$field];

return $this;

/*
$or = $this->persistence->orExpr();
foreach ($field as list($field, $operator, $value)) {
if (is_string($field)) {
$f = $this->hasElement($field);
if (!$f) {
throw new Exception([
'Field does not exist',
'model' => $this,
'field' => $field,
]);
}
} elseif ($field instanceof Field) {
$f = $field;
}
$or->where($f, $operator, $value);
}
return $this;
*/
}

$f = null;
Expand Down
10 changes: 10 additions & 0 deletions src/Persistence_SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ public function initQueryConditions($m, $q)
// parameter inside where()

if (count($cond) == 1) {

// OR conditions
if (is_array($cond[0])) {
foreach ($cond[0] as &$row) {
if (is_string($row[0])) {
$row[0] = $m->getElement($row[0]);
}
}
}

$q->where($cond[0]);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testBasicDiscrimination()
$this->assertEquals(2, count($m->conditions));

$m->addCondition([['gender', 'F'], ['foo', 'bar']]);
$this->assertEquals(4, count($m->conditions));
$this->assertEquals(3, count($m->conditions));
}

public function testEditableAfterCondition()
Expand Down
36 changes: 36 additions & 0 deletions tests/ReferenceSQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ public function testBasicOne()
);
}

/**
* Tests OR conditions.
*/
public function testOrConditions()
{
$a = [
'user' => [
1 => ['id' => 1, 'name' => 'John'],
2 => ['id' => 2, 'name' => 'Peter'],
3 => ['id' => 3, 'name' => 'Joe'],
], 'order' => [
['amount' => '20', 'user_id' => 1],
['amount' => '15', 'user_id' => 2],
['amount' => '5', 'user_id' => 1],
['amount' => '3', 'user_id' => 1],
['amount' => '8', 'user_id' => 3],
], ];
$this->setDB($a);

$db = new Persistence_SQL($this->db->connection);
$u = (new Model($db, 'user'))->addFields(['name']);

$u->addCondition([
['name', 'John'],
['name', 'Peter'],
]);

$this->assertEquals(2, $u->action('count')->getOne());

$u->addCondition([
['name', 'Peter'],
['name', 'Joe'],
]);
$this->assertEquals(1, $u->action('count')->getOne());
}

/**
* Tests Join::addField's ability to create expressions from foreign fields.
*/
Expand Down

0 comments on commit 0a924af

Please sign in to comment.