1 | <?php |
||
17 | trait ActiveQueryTrait |
||
18 | { |
||
19 | /** |
||
20 | * @var string the name of the ActiveRecord class. |
||
21 | */ |
||
22 | public $modelClass; |
||
23 | /** |
||
24 | * @var array a list of relations that this query should be performed with |
||
25 | */ |
||
26 | public $with; |
||
27 | /** |
||
28 | * @var boolean whether to return each record as an array. If false (default), an object |
||
29 | * of [[modelClass]] will be created to represent each record. |
||
30 | */ |
||
31 | public $asArray; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Sets the [[asArray]] property. |
||
36 | * @param boolean $value whether to return the query results in terms of arrays instead of Active Records. |
||
37 | * @return $this the query object itself |
||
38 | */ |
||
39 | 67 | public function asArray($value = true) |
|
44 | |||
45 | /** |
||
46 | * Specifies the relations with which this query should be performed. |
||
47 | * |
||
48 | * The parameters to this method can be either one or multiple strings, or a single array |
||
49 | * of relation names and the optional callbacks to customize the relations. |
||
50 | * |
||
51 | * A relation name can refer to a relation defined in [[modelClass]] |
||
52 | * or a sub-relation that stands for a relation of a related record. |
||
53 | * For example, `orders.address` means the `address` relation defined |
||
54 | * in the model class corresponding to the `orders` relation. |
||
55 | * |
||
56 | * The following are some usage examples: |
||
57 | * |
||
58 | * ```php |
||
59 | * // find customers together with their orders and country |
||
60 | * Customer::find()->with('orders', 'country')->all(); |
||
61 | * // find customers together with their orders and the orders' shipping address |
||
62 | * Customer::find()->with('orders.address')->all(); |
||
63 | * // find customers together with their country and orders of status 1 |
||
64 | * Customer::find()->with([ |
||
65 | * 'orders' => function (\yii\db\ActiveQuery $query) { |
||
66 | * $query->andWhere('status = 1'); |
||
67 | * }, |
||
68 | * 'country', |
||
69 | * ])->all(); |
||
70 | * ``` |
||
71 | * |
||
72 | * You can call `with()` multiple times. Each call will add relations to the existing ones. |
||
73 | * For example, the following two statements are equivalent: |
||
74 | * |
||
75 | * ```php |
||
76 | * Customer::find()->with('orders', 'country')->all(); |
||
77 | * Customer::find()->with('orders')->with('country')->all(); |
||
78 | * ``` |
||
79 | * |
||
80 | * @return $this the query object itself |
||
81 | */ |
||
82 | 49 | public function with() |
|
105 | |||
106 | /** |
||
107 | * Converts found rows into model instances |
||
108 | * @param array $rows |
||
109 | * @return array|ActiveRecord[] |
||
110 | */ |
||
111 | 197 | private function createModels($rows) |
|
153 | |||
154 | /** |
||
155 | * Finds records corresponding to one or multiple relations and populates them into the primary models. |
||
156 | * @param array $with a list of relations that this query should be performed with. Please |
||
157 | * refer to [[with()]] for details about specifying this parameter. |
||
158 | * @param array|ActiveRecord[] $models the primary models (can be either AR instances or arrays) |
||
159 | */ |
||
160 | 46 | public function findWith($with, &$models) |
|
173 | |||
174 | /** |
||
175 | * @param ActiveRecord $model |
||
176 | * @param array $with |
||
177 | * @return ActiveQueryInterface[] |
||
178 | */ |
||
179 | 49 | private function normalizeRelations($model, $with) |
|
212 | } |
||
213 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: