addCondition() public method

This is the most basic for defining condition: ->addCondition('my_field', $value); This condition will work across all persistence drivers universally. In some cases a more complex logic can be used: ->addCondition('my_field', '>', $value); ->addCondition('my_field', '!=', $value); ->addCondition('my_field', 'in', [$value1, $value2]); Second argument could be '=', '>', '<', '>=', '<=', '!=' or 'in'. Those conditions are still supported by most of persistence drivers. There are also vendor-specific expression support: ->addCondition('my_field', $expr); ->addCondition($expr); To use those, you should consult with documentation of your persistence driver.
public addCondition ( mixed $field, mixed $operator = null, mixed $value = null )
$field mixed
$operator mixed
$value mixed
Example #1
0
 public function testEditableAfterCondition()
 {
     $m = new Model();
     $m->addField('name');
     $m->addField('gender');
     $m->addCondition('gender', 'M');
     $this->assertEquals(true, $m->getElement('gender')->system);
     $this->assertEquals(false, $m->getElement('gender')->isEditable());
 }
Example #2
0
 public function testExpressions()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'cached_name' => 'John Smith'], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'cached_name' => 'ERROR']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'user');
     $m->addFields(['name', 'surname', 'cached_name']);
     $m->addExpression('full_name', '[name] || " " || [surname]');
     $m->addCondition($m->expr('[full_name] != [cached_name]'));
     $this->assertEquals('select `id`,`name`,`surname`,`cached_name`,(`name` || " " || `surname`) `full_name` from `user` where (`name` || " " || `surname`) != `cached_name`', $m->action('select')->render());
     $m->tryLoad(1);
     $this->assertEquals(null, $m['name']);
     $m->tryLoad(2);
     $this->assertEquals('Sue', $m['name']);
 }
Example #3
0
 public function testLoadBy()
 {
     $a = ['types' => [['date' => '2013-02-20']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, ['table' => 'types']);
     $m->addField('date', ['type' => 'date', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDate']);
     $m->loadAny();
     $d = $m['date'];
     $m->unload();
     $m->loadBy('date', $d)->unload();
     $m->addCondition('date', $d)->loadAny();
 }