0 フォロワー

トレイト yii\db\ActiveQueryTrait

実装者yii\db\ActiveQuery
利用可能なバージョン2.0
ソースコード https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveQueryTrait.php

ActiveQueryTrait は、アクティブレコードクエリクラスの共通のメソッドとプロパティを実装します。

パブリックプロパティ

継承されたプロパティを隠す

プロパティ 説明 定義元
$asArray boolean 各レコードを配列として返すかどうか。 yii\db\ActiveQueryTrait
$modelClass string アクティブレコードクラスの名前。 yii\db\ActiveQueryTrait
$with array このクエリを実行する必要があるリレーションのリスト yii\db\ActiveQueryTrait

パブリックメソッド

継承されたメソッドを隠す

メソッド 説明 定義元
asArray() asArray() プロパティを設定します。 yii\db\ActiveQueryTrait
findWith() 1つまたは複数のリレーションに対応するレコードを検索し、それらをプライマリモデルに設定します。 yii\db\ActiveQueryTrait
with() このクエリを実行する必要があるリレーションを指定します。 yii\db\ActiveQueryTrait

プロテクトメソッド

継承されたメソッドを隠す

メソッド 説明 定義元
createModels() 見つかった行をモデルインスタンスに変換します。 yii\db\ActiveQueryTrait

プロパティの詳細

継承されたプロパティを隠す

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

各レコードを配列として返すかどうか。false(デフォルト)の場合、各レコードを表すために$modelClassのオブジェクトが作成されます。

public boolean $asArray null
$modelClass パブリックプロパティ

アクティブレコードクラスの名前。

public string $modelClass null
$with パブリックプロパティ

このクエリを実行する必要があるリレーションのリスト

public array $with null

メソッドの詳細

継承されたメソッドを隠す

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

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

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

クエリ結果をアクティブレコードの代わりに配列の形式で返すかどうか。

return $this

クエリオブジェクト自体

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

            
createModels() プロテクトメソッド (バージョン 2.0.11 以降で利用可能)

見つかった行をモデルインスタンスに変換します。

protected array|yii\db\ActiveRecord[] createModels ( $rows )
$rows array

                protected function createModels($rows)
{
    if ($this->asArray) {
        return $rows;
    } else {
        $models = [];
        /* @var $class ActiveRecord */
        $class = $this->modelClass;
        foreach ($rows as $row) {
            $model = $class::instantiate($row);
            $modelClass = get_class($model);
            $modelClass::populateRecord($model, $row);
            $models[] = $model;
        }
        return $models;
    }
}

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

1つまたは複数のリレーションに対応するレコードを検索し、それらをプライマリモデルに設定します。

public void findWith ( $with, &$models )
$with array

このクエリを実行する必要があるリレーションのリスト。このパラメータを指定する方法の詳細については、with()を参照してください。

$models array|yii\db\ActiveRecord[]

プライマリモデル(ARインスタンスまたは配列のいずれか)

                public function findWith($with, &$models)
{
    if (empty($models)) {
        return;
    }
    $primaryModel = reset($models);
    if (!$primaryModel instanceof ActiveRecordInterface) {
        /* @var $modelClass ActiveRecordInterface */
        $modelClass = $this->modelClass;
        $primaryModel = $modelClass::instance();
    }
    $relations = $this->normalizeRelations($primaryModel, $with);
    /* @var $relation ActiveQuery */
    foreach ($relations as $name => $relation) {
        if ($relation->asArray === null) {
            // inherit asArray from primary query
            $relation->asArray($this->asArray);
        }
        $relation->populateRelation($name, $models);
    }
}

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

このクエリを実行する必要があるリレーションを指定します。

このメソッドへのパラメータは、1つまたは複数の文字列、またはリレーション名とリレーションをカスタマイズするためのオプションのコールバックの単一配列のいずれかです。

リレーション名は、$modelClassで定義されているリレーション、または関連レコードのリレーションを表すサブリレーションを参照できます。たとえば、orders.addressは、ordersリレーションに対応するモデルクラスで定義されているaddressリレーションを意味します。

以下にいくつかの使用例を示します。

// find customers together with their orders and country
Customer::find()->with('orders', 'country')->all();
// find customers together with their orders and the orders' shipping address
Customer::find()->with('orders.address')->all();
// find customers together with their country and orders of status 1
Customer::find()->with([
    'orders' => function (\yii\db\ActiveQuery $query) {
        $query->andWhere('status = 1');
    },
    'country',
])->all();

with()を複数回呼び出すことができます。各呼び出しは、既存のリレーションにリレーションを追加します。たとえば、次の2つのステートメントは同等です。

Customer::find()->with('orders', 'country')->all();
Customer::find()->with('orders')->with('country')->all();
public $this with ( )
return $this

クエリオブジェクト自体

                public function with()
{
    $with = func_get_args();
    if (isset($with[0]) && is_array($with[0])) {
        // the parameter is given as an array
        $with = $with[0];
    }
    if (empty($this->with)) {
        $this->with = $with;
    } elseif (!empty($with)) {
        foreach ($with as $name => $value) {
            if (is_int($name)) {
                // repeating relation is fine as normalizeRelations() handle it well
                $this->with[] = $value;
            } else {
                $this->with[$name] = $value;
            }
        }
    }
    return $this;
}