atk4\data\Model::setOrder PHP Method

setOrder() public method

Set order for model records. Multiple calls.
public setOrder ( mixed $field, boolean | null $desc = null )
$field mixed
$desc boolean | null
    public function setOrder($field, $desc = null)
    {
        if (is_string($field) && strpos($field, ',') !== false) {
            $field = explode(',', $field);
        }
        if (is_array($field)) {
            if (!is_null($desc)) {
                throw new Exception(['If first argument is array, second argument must not be used', 'arg1' => $field, 'arg2' => $desc]);
            }
            foreach (array_reverse($field) as $o) {
                $this->setOrder($o);
            }
            return $this;
        }
        if (is_null($desc) && is_string($field)) {
            // no realistic workaround in PHP for 2nd argument being null
            $field = trim($field);
            if (strpos($field, ' ') !== false) {
                list($field, $desc) = array_map('trim', explode(' ', $field, 2));
            }
        }
        $this->order[] = [$field, $desc];
        return $this;
    }

Usage Example

Beispiel #1
0
 /**
  * If first argument is array, then second argument should not be used.
  *
  * @expectedException Exception
  */
 public function testException1()
 {
     $m = new Model();
     $m->addFields(['name', 'salary']);
     $m->setOrder(['name', 'salary'], 'desc');
 }