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 | * @var string collection class to be instantiated and filled with results. |
||
35 | * @since 2.0.10 |
||
36 | */ |
||
37 | public $collectionClass; |
||
38 | |||
39 | /** |
||
40 | * Sets the [[asArray]] property. |
||
41 | * @param boolean $value whether to return the query results in terms of arrays instead of Active Records. |
||
42 | * @return $this the query object itself |
||
43 | */ |
||
44 | 84 | public function asArray($value = true) |
|
49 | |||
50 | /** |
||
51 | * Sets the [[collectionClass]] property. |
||
52 | * @param string $className collection class to be instantiated and filled with results. |
||
53 | * @return $this the query object itself |
||
54 | * @since 2.0.10 |
||
55 | */ |
||
56 | 3 | public function asCollection($className) |
|
61 | |||
62 | /** |
||
63 | * Specifies the relations with which this query should be performed. |
||
64 | * |
||
65 | * The parameters to this method can be either one or multiple strings, or a single array |
||
66 | * of relation names and the optional callbacks to customize the relations. |
||
67 | * |
||
68 | * A relation name can refer to a relation defined in [[modelClass]] |
||
69 | * or a sub-relation that stands for a relation of a related record. |
||
70 | * For example, `orders.address` means the `address` relation defined |
||
71 | * in the model class corresponding to the `orders` relation. |
||
72 | * |
||
73 | * The following are some usage examples: |
||
74 | * |
||
75 | * ```php |
||
76 | * // find customers together with their orders and country |
||
77 | * Customer::find()->with('orders', 'country')->all(); |
||
78 | * // find customers together with their orders and the orders' shipping address |
||
79 | * Customer::find()->with('orders.address')->all(); |
||
80 | * // find customers together with their country and orders of status 1 |
||
81 | * Customer::find()->with([ |
||
82 | * 'orders' => function (\yii\db\ActiveQuery $query) { |
||
83 | * $query->andWhere('status = 1'); |
||
84 | * }, |
||
85 | * 'country', |
||
86 | * ])->all(); |
||
87 | * ``` |
||
88 | * |
||
89 | * You can call `with()` multiple times. Each call will add relations to the existing ones. |
||
90 | * For example, the following two statements are equivalent: |
||
91 | * |
||
92 | * ```php |
||
93 | * Customer::find()->with('orders', 'country')->all(); |
||
94 | * Customer::find()->with('orders')->with('country')->all(); |
||
95 | * ``` |
||
96 | * |
||
97 | * @return $this the query object itself |
||
98 | */ |
||
99 | 78 | public function with() |
|
122 | |||
123 | /** |
||
124 | * Converts found rows into model instances |
||
125 | * @param array $rows |
||
126 | * @return array|ActiveRecord[] |
||
127 | */ |
||
128 | 245 | private function createModels($rows) |
|
170 | |||
171 | /** |
||
172 | * Finds records corresponding to one or multiple relations and populates them into the primary models. |
||
173 | * @param array $with a list of relations that this query should be performed with. Please |
||
174 | * refer to [[with()]] for details about specifying this parameter. |
||
175 | * @param array|ActiveRecord[] $models the primary models (can be either AR instances or arrays) |
||
176 | */ |
||
177 | 63 | public function findWith($with, &$models) |
|
193 | |||
194 | /** |
||
195 | * @param ActiveRecord $model |
||
196 | * @param array $with |
||
197 | * @return ActiveQueryInterface[] |
||
198 | */ |
||
199 | 66 | private function normalizeRelations($model, $with) |
|
232 | } |
||
233 |
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: