Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

216 lines
7.0KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. /**
  9. * ActiveQueryTrait implements the common methods and properties for active record query classes.
  10. *
  11. * @author Qiang Xue <qiang.xue@gmail.com>
  12. * @author Carsten Brandt <mail@cebe.cc>
  13. * @since 2.0
  14. */
  15. trait ActiveQueryTrait
  16. {
  17. /**
  18. * @var string the name of the ActiveRecord class.
  19. */
  20. public $modelClass;
  21. /**
  22. * @var array a list of relations that this query should be performed with
  23. */
  24. public $with;
  25. /**
  26. * @var boolean whether to return each record as an array. If false (default), an object
  27. * of [[modelClass]] will be created to represent each record.
  28. */
  29. public $asArray;
  30. /**
  31. * Sets the [[asArray]] property.
  32. * @param boolean $value whether to return the query results in terms of arrays instead of Active Records.
  33. * @return $this the query object itself
  34. */
  35. public function asArray($value = true)
  36. {
  37. $this->asArray = $value;
  38. return $this;
  39. }
  40. /**
  41. * Specifies the relations with which this query should be performed.
  42. *
  43. * The parameters to this method can be either one or multiple strings, or a single array
  44. * of relation names and the optional callbacks to customize the relations.
  45. *
  46. * A relation name can refer to a relation defined in [[modelClass]]
  47. * or a sub-relation that stands for a relation of a related record.
  48. * For example, `orders.address` means the `address` relation defined
  49. * in the model class corresponding to the `orders` relation.
  50. *
  51. * The following are some usage examples:
  52. *
  53. * ```php
  54. * // find customers together with their orders and country
  55. * Customer::find()->with('orders', 'country')->all();
  56. * // find customers together with their orders and the orders' shipping address
  57. * Customer::find()->with('orders.address')->all();
  58. * // find customers together with their country and orders of status 1
  59. * Customer::find()->with([
  60. * 'orders' => function (\yii\db\ActiveQuery $query) {
  61. * $query->andWhere('status = 1');
  62. * },
  63. * 'country',
  64. * ])->all();
  65. * ```
  66. *
  67. * You can call `with()` multiple times. Each call will add relations to the existing ones.
  68. * For example, the following two statements are equivalent:
  69. *
  70. * ```php
  71. * Customer::find()->with('orders', 'country')->all();
  72. * Customer::find()->with('orders')->with('country')->all();
  73. * ```
  74. *
  75. * @return $this the query object itself
  76. */
  77. public function with()
  78. {
  79. $with = func_get_args();
  80. if (isset($with[0]) && is_array($with[0])) {
  81. // the parameter is given as an array
  82. $with = $with[0];
  83. }
  84. if (empty($this->with)) {
  85. $this->with = $with;
  86. } elseif (!empty($with)) {
  87. foreach ($with as $name => $value) {
  88. if (is_int($name)) {
  89. // repeating relation is fine as normalizeRelations() handle it well
  90. $this->with[] = $value;
  91. } else {
  92. $this->with[$name] = $value;
  93. }
  94. }
  95. }
  96. return $this;
  97. }
  98. /**
  99. * Converts found rows into model instances
  100. * @param array $rows
  101. * @return array|ActiveRecord[]
  102. */
  103. private function createModels($rows)
  104. {
  105. $models = [];
  106. if ($this->asArray) {
  107. if ($this->indexBy === null) {
  108. return $rows;
  109. }
  110. foreach ($rows as $row) {
  111. if (is_string($this->indexBy)) {
  112. $key = $row[$this->indexBy];
  113. } else {
  114. $key = call_user_func($this->indexBy, $row);
  115. }
  116. $models[$key] = $row;
  117. }
  118. } else {
  119. /* @var $class ActiveRecord */
  120. $class = $this->modelClass;
  121. if ($this->indexBy === null) {
  122. foreach ($rows as $row) {
  123. $model = $class::instantiate($row);
  124. $modelClass = get_class($model);
  125. $modelClass::populateRecord($model, $row);
  126. $models[] = $model;
  127. }
  128. } else {
  129. foreach ($rows as $row) {
  130. $model = $class::instantiate($row);
  131. $modelClass = get_class($model);
  132. $modelClass::populateRecord($model, $row);
  133. if (is_string($this->indexBy)) {
  134. $key = $model->{$this->indexBy};
  135. } else {
  136. $key = call_user_func($this->indexBy, $model);
  137. }
  138. $models[$key] = $model;
  139. }
  140. }
  141. }
  142. return $models;
  143. }
  144. /**
  145. * Finds records corresponding to one or multiple relations and populates them into the primary models.
  146. * @param array $with a list of relations that this query should be performed with. Please
  147. * refer to [[with()]] for details about specifying this parameter.
  148. * @param array|ActiveRecord[] $models the primary models (can be either AR instances or arrays)
  149. */
  150. public function findWith($with, &$models)
  151. {
  152. $primaryModel = reset($models);
  153. if (!$primaryModel instanceof ActiveRecordInterface) {
  154. $primaryModel = new $this->modelClass;
  155. }
  156. $relations = $this->normalizeRelations($primaryModel, $with);
  157. /* @var $relation ActiveQuery */
  158. foreach ($relations as $name => $relation) {
  159. if ($relation->asArray === null) {
  160. // inherit asArray from primary query
  161. $relation->asArray($this->asArray);
  162. }
  163. $relation->populateRelation($name, $models);
  164. }
  165. }
  166. /**
  167. * @param ActiveRecord $model
  168. * @param array $with
  169. * @return ActiveQueryInterface[]
  170. */
  171. private function normalizeRelations($model, $with)
  172. {
  173. $relations = [];
  174. foreach ($with as $name => $callback) {
  175. if (is_int($name)) {
  176. $name = $callback;
  177. $callback = null;
  178. }
  179. if (($pos = strpos($name, '.')) !== false) {
  180. // with sub-relations
  181. $childName = substr($name, $pos + 1);
  182. $name = substr($name, 0, $pos);
  183. } else {
  184. $childName = null;
  185. }
  186. if (!isset($relations[$name])) {
  187. $relation = $model->getRelation($name);
  188. $relation->primaryModel = null;
  189. $relations[$name] = $relation;
  190. } else {
  191. $relation = $relations[$name];
  192. }
  193. if (isset($childName)) {
  194. $relation->with[$childName] = $callback;
  195. } elseif ($callback !== null) {
  196. call_user_func($callback, $relation);
  197. }
  198. }
  199. return $relations;
  200. }
  201. }