atk4\data\Model::addCondition PHP Method

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
    public function addCondition($field, $operator = null, $value = null)
    {
        if (is_array($field)) {
            array_map(function ($a) {
                call_user_func_array([$this, 'addCondition'], $a);
            }, $field);
            return $this;
        }
        $f = null;
        // Perform basic validation to see if the field exists
        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;
        }
        if ($f) {
            $f->system = true;
            if ($operator === '=' || func_num_args() == 2) {
                $v = $operator === '=' ? $value : $operator;
                if (!is_object($v) && !is_array($v)) {
                    $f->default = $v;
                }
            }
        }
        $this->conditions[] = func_get_args();
        return $this;
    }

Usage Example

Beispiel #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());
 }
All Usage Examples Of atk4\data\Model::addCondition