yii\sphinx\Query::addWithin PHP Method

addWithin() public method

Adds additional WITHIN GROUP ORDER BY columns to the query.
See also: within()
public addWithin ( string | array $columns )
$columns string | array the columns (and the directions) to find best row within a group. Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. `['id' => Query::SORT_ASC, 'name' => Query::SORT_DESC]`). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression).
    public function addWithin($columns)
    {
        $columns = $this->normalizeOrderBy($columns);
        if ($this->within === null) {
            $this->within = $columns;
        } else {
            $this->within = array_merge($this->within, $columns);
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testWithin()
 {
     $query = new Query();
     $query->within('team');
     $this->assertEquals(['team' => SORT_ASC], $query->within);
     $query->addWithin('company');
     $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->within);
     $query->addWithin('age');
     $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->within);
     $query->addWithin(['age' => SORT_DESC]);
     $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->within);
     $query->addWithin('age ASC, company DESC');
     $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->within);
 }