0 フォロワー

トレイト yii\db\QueryTrait

実装クラスyii\db\ActiveQueryyii\db\Query
利用可能バージョン2.0
ソースコード https://github.com/yiisoft/yii2/blob/master/framework/db/QueryTrait.php

BaseQuery トレイトは、データベースクエリに必要な最小限のメソッドセットを表します。

yii\db\QueryInterface を実装するクラスで使用することを想定しています。

公開プロパティ

継承されたプロパティを非表示

プロパティ 説明 定義元
$emulateExecution boolean 実際のクエリ実行をエミュレートし、空の結果またはfalseを返すかどうか。 yii\db\QueryTrait
$indexBy string|callable|null クエリ結果をインデックス付ける列の名前。与えられた行データに基づいてインデックス値を返すcallable(例:無名関数)も指定できます。indexBy()を参照ください。このプロパティはall()によってのみ使用されます。 yii\db\QueryTrait
$limit integer|yii\db\ExpressionInterface|null 返されるレコードの最大数。 yii\db\QueryTrait
$offset integer|yii\db\ExpressionInterface|null 0から始まる、レコードを返す開始位置。 yii\db\QueryTrait
$orderBy array|null クエリ結果のソート方法。 yii\db\QueryTrait
$where string|array|yii\db\ExpressionInterface|null クエリ条件。 yii\db\QueryTrait

公開メソッド

継承されたメソッドを非表示

メソッド 説明 定義元
addOrderBy() クエリにORDER BY句を追加します。 yii\db\QueryTrait
andFilterWhere() 既存のWHERE条件に追加しますが、空のオペランドは無視します。 yii\db\QueryTrait
andWhere() 既存のWHERE条件に追加します。 yii\db\QueryTrait
emulateExecution() クエリの実行をエミュレートし、データストアとのやり取りを防止するかどうかを設定します。 yii\db\QueryTrait
filterWhere() WHERE句を設定しますが、空のオペランドは無視します。 yii\db\QueryTrait
indexBy() indexBy()プロパティを設定します。 yii\db\QueryTrait
limit() クエリのLIMIT句を設定します。 yii\db\QueryTrait
offset() クエリのOFFSET句を設定します。 yii\db\QueryTrait
orFilterWhere() 既存のWHERE条件に追加しますが、空のオペランドは無視します。 yii\db\QueryTrait
orWhere() 既存のWHERE条件に追加します。 yii\db\QueryTrait
orderBy() クエリのORDER BY句を設定します。 yii\db\QueryTrait
where() クエリのWHERE句を設定します。 yii\db\QueryTrait

保護メソッド

継承されたメソッドを非表示

メソッド 説明 定義元
filterCondition() 与えられたクエリ条件から空のオペランドを削除します。 yii\db\QueryTrait
isEmpty() 与えられた値が「空」かどうかを示す値を返します。 yii\db\QueryTrait
normalizeOrderBy() ORDER BYデータの形式を正規化します。 yii\db\QueryTrait

プロパティの詳細

継承されたプロパティを非表示

$emulateExecution パブリックプロパティ (バージョン2.0.11から利用可能)

実際のクエリ実行をエミュレートし、空の結果またはfalseを返すかどうか。

emulateExecution()も参照してください。

public boolean $emulateExecution false
$indexBy パブリックプロパティ

クエリ結果をインデックス付ける列の名前。これは、指定された行データに基づいてインデックス値を返すcallable(例:無名関数)も指定できます。indexBy()を参照ください。このプロパティはall()によってのみ使用されます。

public string|callable|null $indexBy null
$limit パブリックプロパティ

返されるレコードの最大数。yii\db\ExpressionInterfaceのインスタンスも指定できます。設定されていないか、0以下の場合は、制限なしを意味します。

$offset パブリックプロパティ

0から始まる、レコードを返す開始位置。yii\db\ExpressionInterfaceのインスタンスも指定できます。設定されていないか、0以下の場合は、先頭から開始することを意味します。

$orderBy パブリックプロパティ

クエリ結果のソート方法を指定します。これは、SQL文のORDER BY句を構築するために使用されます。配列のキーはソート対象の列名、値はソート順で、SORT_ASCまたはSORT_DESCを指定できます。yii\db\ExpressionInterfaceオブジェクトを含めることもできます。その場合、式は変更なしで文字列に変換されます。

public array|null $orderBy null
$where パブリックプロパティ

クエリ条件。SQL文のWHERE句に対応します。例えば、`['age' => 31, 'team' => 1]` のようになります。

この値の指定方法については、where()も参照してください。

メソッドの詳細

継承されたメソッドを非表示

addOrderBy() パブリックメソッド

クエリにORDER BY句を追加します。

orderBy()も参照してください。

public $this addOrderBy ( $columns )
$columns string|array|yii\db\ExpressionInterface

ソート対象の列(と方向)。列は文字列(例:"id ASC, name DESC")または配列(例:`['id' => SORT_ASC, 'name' => SORT_DESC]`)で指定できます。

列名に括弧が含まれていない限り(DB式を含む場合)、メソッドは自動的に列名を引用符で囲みます。

カンマを含む式でソートする場合、常に配列を使用してソート情報を表す必要があります。そうでない場合、メソッドはソート列を正しく判別できません。

バージョン2.0.7以降、yii\db\ExpressionInterfaceオブジェクトを渡して、ORDER BY部分をプレーンなSQLで明示的に指定できます。

戻り値 $this

クエリオブジェクト自体

                public function addOrderBy($columns)
{
    $columns = $this->normalizeOrderBy($columns);
    if ($this->orderBy === null) {
        $this->orderBy = $columns;
    } else {
        $this->orderBy = array_merge($this->orderBy, $columns);
    }
    return $this;
}

            
andFilterWhere() パブリックメソッド

既存のWHERE条件に追加しますが、空のオペランドは無視します。

新しい条件と既存の条件は、「AND」演算子を使用して結合されます。

このメソッドはandWhere()に似ています。主な違いは、このメソッドが空のクエリオペランドを削除することです。そのため、このメソッドはユーザーが入力したフィルタ値に基づいてクエリ条件を構築するのに最適です。

こちらも参照

public $this andFilterWhere ( array $condition )
$condition array

新しいWHERE条件。where()でこのパラメータの指定方法を参照してください。

戻り値 $this

クエリオブジェクト自体

                public function andFilterWhere(array $condition)
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->andWhere($condition);
    }
    return $this;
}

            
andWhere() パブリックメソッド

既存のWHERE条件に追加します。

新しい条件と既存の条件は、「AND」演算子を使用して結合されます。

こちらも参照

public $this andWhere ( $condition )
$condition string|array|yii\db\ExpressionInterface

新しいWHERE条件。where()でこのパラメータの指定方法を参照してください。

戻り値 $this

クエリオブジェクト自体

                public function andWhere($condition)
{
    if ($this->where === null) {
        $this->where = $condition;
    } else {
        $this->where = ['and', $this->where, $condition];
    }
    return $this;
}

            
emulateExecution() パブリックメソッド (バージョン 2.0.11 から利用可能)

クエリの実行をエミュレートし、データストアとのやり取りを防止するかどうかを設定します。

このモードを有効にすると、yii\db\QueryInterface::one()yii\db\QueryInterface::all()yii\db\QueryInterface::exists()など、クエリ結果を返すメソッドは、空の値またはfalseを返します。 `0=1` のようなfalseとなる条件を設定した場合など、プログラムロジックでクエリが結果を返す必要がない場合に、このメソッドを使用する必要があります。

public $this emulateExecution ( $value true )
$value boolean

クエリの実行を防止するかどうか。

戻り値 $this

クエリオブジェクト自体。

                public function emulateExecution($value = true)
{
    $this->emulateExecution = $value;
    return $this;
}

            
filterCondition() プロテクトメソッド

与えられたクエリ条件から空のオペランドを削除します。

protected array filterCondition ( $condition )
$condition array

元の条件

戻り値 array

空のオペランドが削除された条件。

例外 yii\base\NotSupportedException

条件演算子がサポートされていない場合

                protected function filterCondition($condition)
{
    if (!is_array($condition)) {
        return $condition;
    }
    if (!isset($condition[0])) {
        // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
        foreach ($condition as $name => $value) {
            if ($this->isEmpty($value)) {
                unset($condition[$name]);
            }
        }
        return $condition;
    }
    // operator format: operator, operand 1, operand 2, ...
    $operator = array_shift($condition);
    switch (strtoupper($operator)) {
        case 'NOT':
        case 'AND':
        case 'OR':
            foreach ($condition as $i => $operand) {
                $subCondition = $this->filterCondition($operand);
                if ($this->isEmpty($subCondition)) {
                    unset($condition[$i]);
                } else {
                    $condition[$i] = $subCondition;
                }
            }
            if (empty($condition)) {
                return [];
            }
            break;
        case 'BETWEEN':
        case 'NOT BETWEEN':
            if (array_key_exists(1, $condition) && array_key_exists(2, $condition)) {
                if ($this->isEmpty($condition[1]) || $this->isEmpty($condition[2])) {
                    return [];
                }
            }
            break;
        default:
            if (array_key_exists(1, $condition) && $this->isEmpty($condition[1])) {
                return [];
            }
    }
    array_unshift($condition, $operator);
    return $condition;
}

            
filterWhere() パブリックメソッド

WHERE句を設定しますが、空のオペランドは無視します。

このメソッドはwhere()に似ています。主な違いは、このメソッドが空のクエリオペランドを削除することです。そのため、このメソッドはユーザーが入力したフィルタ値に基づいてクエリ条件を構築するのに最適です。

次のコードは、このメソッドとwhere()の違いを示しています。

// WHERE `age`=:age
$query->filterWhere(['name' => null, 'age' => 20]);
// WHERE `age`=:age
$query->where(['age' => 20]);
// WHERE `name` IS NULL AND `age`=:age
$query->where(['name' => null, 'age' => 20]);

where()とは異なり、このメソッドにはバインディングパラメータを渡すことができません。

こちらも参照

public $this filterWhere ( array $condition )
$condition array

WHERE句に追加する条件。where()でこのパラメータの指定方法を参照してください。

戻り値 $this

クエリオブジェクト自体

                public function filterWhere(array $condition)
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->where($condition);
    }
    return $this;
}

            
indexBy() パブリックメソッド

indexBy()プロパティを設定します。

public $this indexBy ( $column )
$column string|callable

クエリ結果をインデックスするために使用する列の名前。これは、指定された行データに基づいてインデックス値を返すコールバック関数(例:無名関数)にすることもできます。コールバック関数のシグネチャは次のようになります。

function ($row)
{
    // return the index value corresponding to $row
}
戻り値 $this

クエリオブジェクト自体

                public function indexBy($column)
{
    $this->indexBy = $column;
    return $this;
}

            
isEmpty() protectedメソッド

与えられた値が「空」かどうかを示す値を返します。

以下の条件のいずれかが満たされた場合、値は「空」とみなされます。

  • nullである場合、
  • 空文字列('')である場合、
  • 空白文字のみを含む文字列である場合、
  • 空の配列である場合。
protected boolean isEmpty ( $value )
$value 混合型
戻り値 boolean

値が空の場合

                protected function isEmpty($value)
{
    return $value === '' || $value === [] || $value === null || is_string($value) && trim($value) === '';
}

            
limit() publicメソッド

クエリのLIMIT句を設定します。

public $this limit ( $limit )
$limit integer|yii\db\ExpressionInterface|null

制限数。制限を無効にするには、nullまたは負の値を使用します。

戻り値 $this

クエリオブジェクト自体

                public function limit($limit)
{
    $this->limit = $limit;
    return $this;
}

            
normalizeOrderBy() protectedメソッド

ORDER BYデータの形式を正規化します。

protected array normalizeOrderBy ( $columns )
$columns array|string|yii\db\ExpressionInterface|null

正規化する列の値。orderBy()addOrderBy()を参照してください。

                protected function normalizeOrderBy($columns)
{
    if (empty($columns)) {
        return [];
    } elseif ($columns instanceof ExpressionInterface) {
        return [$columns];
    } elseif (is_array($columns)) {
        return $columns;
    }
    $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
    $result = [];
    foreach ($columns as $column) {
        if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) {
            $result[$matches[1]] = strcasecmp($matches[2], 'desc') ? SORT_ASC : SORT_DESC;
        } else {
            $result[$column] = SORT_ASC;
        }
    }
    return $result;
}

            
offset() publicメソッド

クエリのOFFSET句を設定します。

public $this offset ( $offset )
$offset integer|yii\db\ExpressionInterface|null

オフセット。オフセットを無効にするには、nullまたは負の値を使用します。

戻り値 $this

クエリオブジェクト自体

                public function offset($offset)
{
    $this->offset = $offset;
    return $this;
}

            
orFilterWhere() publicメソッド

既存のWHERE条件に追加しますが、空のオペランドは無視します。

新しい条件と既存の条件は、「OR」演算子を使用して結合されます。

このメソッドはorWhere()に似ています。主な違いは、このメソッドが空のクエリオペランドを削除することです。そのため、このメソッドは、ユーザーが入力したフィルター値に基づいてクエリ条件を構築するのに最適です。

こちらも参照

public $this orFilterWhere ( array $condition )
$condition array

新しいWHERE条件。where()でこのパラメータの指定方法を参照してください。

戻り値 $this

クエリオブジェクト自体

                public function orFilterWhere(array $condition)
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->orWhere($condition);
    }
    return $this;
}

            
orWhere() publicメソッド

既存のWHERE条件に追加します。

新しい条件と既存の条件は、「OR」演算子を使用して結合されます。

こちらも参照

public $this orWhere ( $condition )
$condition string|array|yii\db\ExpressionInterface

新しいWHERE条件。where()でこのパラメータの指定方法を参照してください。

戻り値 $this

クエリオブジェクト自体

                public function orWhere($condition)
{
    if ($this->where === null) {
        $this->where = $condition;
    } else {
        $this->where = ['or', $this->where, $condition];
    }
    return $this;
}

            
orderBy() publicメソッド

クエリのORDER BY句を設定します。

addOrderBy()も参照してください。

public $this orderBy ( $columns )
$columns string|array|yii\db\ExpressionInterface|null

ソート順序を指定する列(と方向)。列は、文字列(例:"id ASC, name DESC")または配列(例:['id' => SORT_ASC, 'name' => SORT_DESC])で指定できます。

列名に括弧が含まれていない限り(DB式を含む場合)、メソッドは自動的に列名を引用符で囲みます。

カンマを含む式でソートする場合、常に配列を使用してソート情報を表す必要があります。そうでない場合、メソッドはソート列を正しく判別できません。

バージョン2.0.7以降、yii\db\ExpressionInterfaceオブジェクトを渡して、ORDER BY部分をプレーンなSQLで明示的に指定できます。

戻り値 $this

クエリオブジェクト自体

                public function orderBy($columns)
{
    $this->orderBy = $this->normalizeOrderBy($columns);
    return $this;
}

            
where() publicメソッド

クエリのWHERE句を設定します。

yii\db\QueryInterface::where()の詳細なドキュメントを参照してください。

こちらも参照

public $this where ( $condition )
$condition string|array|yii\db\ExpressionInterface

WHERE句に追加する条件。

戻り値 $this

クエリオブジェクト自体

                public function where($condition)
{
    $this->where = $condition;
    return $this;
}