0 追蹤者

Trait yii\db\ActiveQueryTrait

實作於yii\db\ActiveQuery
版本2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveQueryTrait.php

ActiveQueryTrait 實作了 active record 查詢類別的通用方法和屬性。

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$asArray boolean 是否將每筆記錄作為陣列回傳。 yii\db\ActiveQueryTrait
$modelClass string ActiveRecord 類別的名稱。 yii\db\ActiveQueryTrait
$with array 此查詢應執行的關聯列表 yii\db\ActiveQueryTrait

公開方法

隱藏繼承的方法

方法 描述 定義於
asArray() 設定 asArray() 屬性。 yii\db\ActiveQueryTrait
findWith() 尋找對應於一個或多個關聯的記錄,並將其填充到主要模型中。 yii\db\ActiveQueryTrait
with() 指定此查詢應執行的關聯。 yii\db\ActiveQueryTrait

受保護的方法

隱藏繼承的方法

方法 描述 定義於
createModels() 將找到的列轉換為模型實例。 yii\db\ActiveQueryTrait

屬性詳細資訊

隱藏繼承的屬性

$asArray public property

是否將每個記錄作為陣列返回。如果為 false(預設),將會建立 $modelClass 的物件來表示每個記錄。

public boolean $asArray null
$modelClass public property

ActiveRecord 類別的名稱。

public string $modelClass null
$with public property

此查詢應執行的關聯列表

public array $with null

方法詳情

隱藏繼承的方法

asArray() public method

設定 asArray() 屬性。

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

是否以陣列形式而不是 Active Record 的形式返回查詢結果。

return $this

查詢物件本身

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

            
createModels() protected method (available since version 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() public method

尋找對應於一個或多個關聯的記錄,並將其填充到主要模型中。

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() public method

指定此查詢應執行的關聯。

此方法的參數可以是一個或多個字串,或包含關聯名稱和自訂關聯的可選回呼的單個陣列。

關聯名稱可以參考 $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()。每次呼叫都會將關聯新增到現有的關聯中。例如,以下兩個語句是等效的

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;
}