Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

521 line
19KB

  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. use yii\base\InvalidConfigException;
  9. use yii\base\InvalidParamException;
  10. /**
  11. * ActiveRelationTrait implements the common methods and properties for active record relational queries.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @author Carsten Brandt <mail@cebe.cc>
  15. * @since 2.0
  16. *
  17. * @method ActiveRecordInterface one()
  18. * @method ActiveRecordInterface[] all()
  19. * @property ActiveRecord $modelClass
  20. */
  21. trait ActiveRelationTrait
  22. {
  23. /**
  24. * @var boolean whether this query represents a relation to more than one record.
  25. * This property is only used in relational context. If true, this relation will
  26. * populate all query results into AR instances using [[Query::all()|all()]].
  27. * If false, only the first row of the results will be retrieved using [[Query::one()|one()]].
  28. */
  29. public $multiple;
  30. /**
  31. * @var ActiveRecord the primary model of a relational query.
  32. * This is used only in lazy loading with dynamic query options.
  33. */
  34. public $primaryModel;
  35. /**
  36. * @var array the columns of the primary and foreign tables that establish a relation.
  37. * The array keys must be columns of the table for this relation, and the array values
  38. * must be the corresponding columns from the primary table.
  39. * Do not prefix or quote the column names as this will be done automatically by Yii.
  40. * This property is only used in relational context.
  41. */
  42. public $link;
  43. /**
  44. * @var array|object the query associated with the junction table. Please call [[via()]]
  45. * to set this property instead of directly setting it.
  46. * This property is only used in relational context.
  47. * @see via()
  48. */
  49. public $via;
  50. /**
  51. * @var string the name of the relation that is the inverse of this relation.
  52. * For example, an order has a customer, which means the inverse of the "customer" relation
  53. * is the "orders", and the inverse of the "orders" relation is the "customer".
  54. * If this property is set, the primary record(s) will be referenced through the specified relation.
  55. * For example, `$customer->orders[0]->customer` and `$customer` will be the same object,
  56. * and accessing the customer of an order will not trigger new DB query.
  57. * This property is only used in relational context.
  58. * @see inverseOf()
  59. */
  60. public $inverseOf;
  61. /**
  62. * Clones internal objects.
  63. */
  64. public function __clone()
  65. {
  66. parent::__clone();
  67. // make a clone of "via" object so that the same query object can be reused multiple times
  68. if (is_object($this->via)) {
  69. $this->via = clone $this->via;
  70. } elseif (is_array($this->via)) {
  71. $this->via = [$this->via[0], clone $this->via[1]];
  72. }
  73. }
  74. /**
  75. * Specifies the relation associated with the junction table.
  76. *
  77. * Use this method to specify a pivot record/table when declaring a relation in the [[ActiveRecord]] class:
  78. *
  79. * ```php
  80. * public function getOrders()
  81. * {
  82. * return $this->hasOne(Order::className(), ['id' => 'order_id']);
  83. * }
  84. *
  85. * public function getOrderItems()
  86. * {
  87. * return $this->hasMany(Item::className(), ['id' => 'item_id'])
  88. * ->via('orders');
  89. * }
  90. * ```
  91. *
  92. * @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]].
  93. * @param callable $callable a PHP callback for customizing the relation associated with the junction table.
  94. * Its signature should be `function($query)`, where `$query` is the query to be customized.
  95. * @return static the relation object itself.
  96. */
  97. public function via($relationName, callable $callable = null)
  98. {
  99. $relation = $this->primaryModel->getRelation($relationName);
  100. $this->via = [$relationName, $relation];
  101. if ($callable !== null) {
  102. call_user_func($callable, $relation);
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Sets the name of the relation that is the inverse of this relation.
  108. * For example, an order has a customer, which means the inverse of the "customer" relation
  109. * is the "orders", and the inverse of the "orders" relation is the "customer".
  110. * If this property is set, the primary record(s) will be referenced through the specified relation.
  111. * For example, `$customer->orders[0]->customer` and `$customer` will be the same object,
  112. * and accessing the customer of an order will not trigger a new DB query.
  113. *
  114. * Use this method when declaring a relation in the [[ActiveRecord]] class:
  115. *
  116. * ```php
  117. * public function getOrders()
  118. * {
  119. * return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer');
  120. * }
  121. * ```
  122. *
  123. * @param string $relationName the name of the relation that is the inverse of this relation.
  124. * @return static the relation object itself.
  125. */
  126. public function inverseOf($relationName)
  127. {
  128. $this->inverseOf = $relationName;
  129. return $this;
  130. }
  131. /**
  132. * Finds the related records for the specified primary record.
  133. * This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
  134. * @param string $name the relation name
  135. * @param ActiveRecordInterface|BaseActiveRecord $model the primary model
  136. * @return mixed the related record(s)
  137. * @throws InvalidParamException if the relation is invalid
  138. */
  139. public function findFor($name, $model)
  140. {
  141. if (method_exists($model, 'get' . $name)) {
  142. $method = new \ReflectionMethod($model, 'get' . $name);
  143. $realName = lcfirst(substr($method->getName(), 3));
  144. if ($realName !== $name) {
  145. throw new InvalidParamException('Relation names are case sensitive. ' . get_class($model) . " has a relation named \"$realName\" instead of \"$name\".");
  146. }
  147. }
  148. $related = $this->multiple ? $this->all() : $this->one();
  149. if ($this->inverseOf === null || empty($related)) {
  150. return $related;
  151. }
  152. $inverseRelation = (new $this->modelClass)->getRelation($this->inverseOf);
  153. if ($this->multiple) {
  154. foreach ($related as $i => $relatedModel) {
  155. if ($relatedModel instanceof ActiveRecordInterface) {
  156. $relatedModel->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$model] : $model);
  157. } else {
  158. $related[$i][$this->inverseOf] = $inverseRelation->multiple ? [$model] : $model;
  159. }
  160. }
  161. } else {
  162. if ($related instanceof ActiveRecordInterface) {
  163. $related->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$model] : $model);
  164. } else {
  165. $related[$this->inverseOf] = $inverseRelation->multiple ? [$model] : $model;
  166. }
  167. }
  168. return $related;
  169. }
  170. /**
  171. * Finds the related records and populates them into the primary models.
  172. * @param string $name the relation name
  173. * @param array $primaryModels primary models
  174. * @return array the related models
  175. * @throws InvalidConfigException if [[link]] is invalid
  176. */
  177. public function populateRelation($name, &$primaryModels)
  178. {
  179. if (!is_array($this->link)) {
  180. throw new InvalidConfigException('Invalid link: it must be an array of key-value pairs.');
  181. }
  182. if ($this->via instanceof self) {
  183. // via junction table
  184. /* @var $viaQuery ActiveRelationTrait */
  185. $viaQuery = $this->via;
  186. $viaModels = $viaQuery->findJunctionRows($primaryModels);
  187. $this->filterByModels($viaModels);
  188. } elseif (is_array($this->via)) {
  189. // via relation
  190. /* @var $viaQuery ActiveRelationTrait|ActiveQueryTrait */
  191. list($viaName, $viaQuery) = $this->via;
  192. if ($viaQuery->asArray === null) {
  193. // inherit asArray from primary query
  194. $viaQuery->asArray($this->asArray);
  195. }
  196. $viaQuery->primaryModel = null;
  197. $viaModels = $viaQuery->populateRelation($viaName, $primaryModels);
  198. $this->filterByModels($viaModels);
  199. } else {
  200. $this->filterByModels($primaryModels);
  201. }
  202. if (!$this->multiple && count($primaryModels) === 1) {
  203. $model = $this->one();
  204. foreach ($primaryModels as $i => $primaryModel) {
  205. if ($primaryModel instanceof ActiveRecordInterface) {
  206. $primaryModel->populateRelation($name, $model);
  207. } else {
  208. $primaryModels[$i][$name] = $model;
  209. }
  210. if ($this->inverseOf !== null) {
  211. $this->populateInverseRelation($primaryModels, [$model], $name, $this->inverseOf);
  212. }
  213. }
  214. return [$model];
  215. } else {
  216. // https://github.com/yiisoft/yii2/issues/3197
  217. // delay indexing related models after buckets are built
  218. $indexBy = $this->indexBy;
  219. $this->indexBy = null;
  220. $models = $this->all();
  221. if (isset($viaModels, $viaQuery)) {
  222. $buckets = $this->buildBuckets($models, $this->link, $viaModels, $viaQuery->link);
  223. } else {
  224. $buckets = $this->buildBuckets($models, $this->link);
  225. }
  226. $this->indexBy = $indexBy;
  227. if ($this->indexBy !== null && $this->multiple) {
  228. $buckets = $this->indexBuckets($buckets, $this->indexBy);
  229. }
  230. $link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link);
  231. foreach ($primaryModels as $i => $primaryModel) {
  232. if ($this->multiple && count($link) == 1 && is_array($keys = $primaryModel[reset($link)])) {
  233. $value = [];
  234. foreach ($keys as $key) {
  235. if (!is_scalar($key)) {
  236. $key = serialize($key);
  237. }
  238. if (isset($buckets[$key])) {
  239. if ($this->indexBy !== null) {
  240. // if indexBy is set, array_merge will cause renumbering of numeric array
  241. foreach($buckets[$key] as $bucketKey => $bucketValue) {
  242. $value[$bucketKey] = $bucketValue;
  243. }
  244. } else {
  245. $value = array_merge($value, $buckets[$key]);
  246. }
  247. }
  248. }
  249. } else {
  250. $key = $this->getModelKey($primaryModel, $link);
  251. $value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? [] : null);
  252. }
  253. if ($primaryModel instanceof ActiveRecordInterface) {
  254. $primaryModel->populateRelation($name, $value);
  255. } else {
  256. $primaryModels[$i][$name] = $value;
  257. }
  258. }
  259. if ($this->inverseOf !== null) {
  260. $this->populateInverseRelation($primaryModels, $models, $name, $this->inverseOf);
  261. }
  262. return $models;
  263. }
  264. }
  265. /**
  266. * @param ActiveRecordInterface[] $primaryModels primary models
  267. * @param ActiveRecordInterface[] $models models
  268. * @param string $primaryName the primary relation name
  269. * @param string $name the relation name
  270. */
  271. private function populateInverseRelation(&$primaryModels, $models, $primaryName, $name)
  272. {
  273. if (empty($models) || empty($primaryModels)) {
  274. return;
  275. }
  276. $model = reset($models);
  277. /* @var $relation ActiveQueryInterface|ActiveQuery */
  278. $relation = $model instanceof ActiveRecordInterface ? $model->getRelation($name) : (new $this->modelClass)->getRelation($name);
  279. if ($relation->multiple) {
  280. $buckets = $this->buildBuckets($primaryModels, $relation->link, null, null, false);
  281. if ($model instanceof ActiveRecordInterface) {
  282. foreach ($models as $model) {
  283. $key = $this->getModelKey($model, $relation->link);
  284. $model->populateRelation($name, isset($buckets[$key]) ? $buckets[$key] : []);
  285. }
  286. } else {
  287. foreach ($primaryModels as $i => $primaryModel) {
  288. if ($this->multiple) {
  289. foreach ($primaryModel as $j => $m) {
  290. $key = $this->getModelKey($m, $relation->link);
  291. $primaryModels[$i][$j][$name] = isset($buckets[$key]) ? $buckets[$key] : [];
  292. }
  293. } elseif (!empty($primaryModel[$primaryName])) {
  294. $key = $this->getModelKey($primaryModel[$primaryName], $relation->link);
  295. $primaryModels[$i][$primaryName][$name] = isset($buckets[$key]) ? $buckets[$key] : [];
  296. }
  297. }
  298. }
  299. } else {
  300. if ($this->multiple) {
  301. foreach ($primaryModels as $i => $primaryModel) {
  302. foreach ($primaryModel[$primaryName] as $j => $m) {
  303. if ($m instanceof ActiveRecordInterface) {
  304. $m->populateRelation($name, $primaryModel);
  305. } else {
  306. $primaryModels[$i][$primaryName][$j][$name] = $primaryModel;
  307. }
  308. }
  309. }
  310. } else {
  311. foreach ($primaryModels as $i => $primaryModel) {
  312. if ($primaryModels[$i][$primaryName] instanceof ActiveRecordInterface) {
  313. $primaryModels[$i][$primaryName]->populateRelation($name, $primaryModel);
  314. } elseif (!empty($primaryModels[$i][$primaryName])) {
  315. $primaryModels[$i][$primaryName][$name] = $primaryModel;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. /**
  322. * @param array $models
  323. * @param array $link
  324. * @param array $viaModels
  325. * @param array $viaLink
  326. * @param boolean $checkMultiple
  327. * @return array
  328. */
  329. private function buildBuckets($models, $link, $viaModels = null, $viaLink = null, $checkMultiple = true)
  330. {
  331. if ($viaModels !== null) {
  332. $map = [];
  333. $viaLinkKeys = array_keys($viaLink);
  334. $linkValues = array_values($link);
  335. foreach ($viaModels as $viaModel) {
  336. $key1 = $this->getModelKey($viaModel, $viaLinkKeys);
  337. $key2 = $this->getModelKey($viaModel, $linkValues);
  338. $map[$key2][$key1] = true;
  339. }
  340. }
  341. $buckets = [];
  342. $linkKeys = array_keys($link);
  343. if (isset($map)) {
  344. foreach ($models as $i => $model) {
  345. $key = $this->getModelKey($model, $linkKeys);
  346. if (isset($map[$key])) {
  347. foreach (array_keys($map[$key]) as $key2) {
  348. $buckets[$key2][] = $model;
  349. }
  350. }
  351. }
  352. } else {
  353. foreach ($models as $i => $model) {
  354. $key = $this->getModelKey($model, $linkKeys);
  355. $buckets[$key][] = $model;
  356. }
  357. }
  358. if ($checkMultiple && !$this->multiple) {
  359. foreach ($buckets as $i => $bucket) {
  360. $buckets[$i] = reset($bucket);
  361. }
  362. }
  363. return $buckets;
  364. }
  365. /**
  366. * Indexes buckets by column name.
  367. *
  368. * @param array $buckets
  369. * @var string|callable $column the name of the column by which the query results should be indexed by.
  370. * This can also be a callable (e.g. anonymous function) that returns the index value based on the given row data.
  371. * @return array
  372. */
  373. private function indexBuckets($buckets, $indexBy)
  374. {
  375. $result = [];
  376. foreach ($buckets as $key => $models) {
  377. $result[$key] = [];
  378. foreach ($models as $model) {
  379. $index = is_string($indexBy) ? $model[$indexBy] : call_user_func($indexBy, $model);
  380. $result[$key][$index] = $model;
  381. }
  382. }
  383. return $result;
  384. }
  385. /**
  386. * @param array $attributes the attributes to prefix
  387. * @return array
  388. */
  389. private function prefixKeyColumns($attributes)
  390. {
  391. if ($this instanceof ActiveQuery && (!empty($this->join) || !empty($this->joinWith))) {
  392. if (empty($this->from)) {
  393. /* @var $modelClass ActiveRecord */
  394. $modelClass = $this->modelClass;
  395. $alias = $modelClass::tableName();
  396. } else {
  397. foreach ($this->from as $alias => $table) {
  398. if (!is_string($alias)) {
  399. $alias = $table;
  400. }
  401. break;
  402. }
  403. }
  404. if (isset($alias)) {
  405. foreach ($attributes as $i => $attribute) {
  406. $attributes[$i] = "$alias.$attribute";
  407. }
  408. }
  409. }
  410. return $attributes;
  411. }
  412. /**
  413. * @param array $models
  414. */
  415. private function filterByModels($models)
  416. {
  417. $attributes = array_keys($this->link);
  418. $attributes = $this->prefixKeyColumns($attributes);
  419. $values = [];
  420. if (count($attributes) === 1) {
  421. // single key
  422. $attribute = reset($this->link);
  423. foreach ($models as $model) {
  424. if (($value = $model[$attribute]) !== null) {
  425. if (is_array($value)) {
  426. $values = array_merge($values, $value);
  427. } else {
  428. $values[] = $value;
  429. }
  430. }
  431. }
  432. } else {
  433. // composite keys
  434. foreach ($models as $model) {
  435. $v = [];
  436. foreach ($this->link as $attribute => $link) {
  437. $v[$attribute] = $model[$link];
  438. }
  439. $values[] = $v;
  440. }
  441. }
  442. $this->andWhere(['in', $attributes, array_unique($values, SORT_REGULAR)]);
  443. }
  444. /**
  445. * @param ActiveRecord|array $model
  446. * @param array $attributes
  447. * @return string
  448. */
  449. private function getModelKey($model, $attributes)
  450. {
  451. if (count($attributes) > 1) {
  452. $key = [];
  453. foreach ($attributes as $attribute) {
  454. $key[] = $model[$attribute];
  455. }
  456. return serialize($key);
  457. } else {
  458. $attribute = reset($attributes);
  459. $key = $model[$attribute];
  460. return is_scalar($key) ? $key : serialize($key);
  461. }
  462. }
  463. /**
  464. * @param array $primaryModels either array of AR instances or arrays
  465. * @return array
  466. */
  467. private function findJunctionRows($primaryModels)
  468. {
  469. if (empty($primaryModels)) {
  470. return [];
  471. }
  472. $this->filterByModels($primaryModels);
  473. /* @var $primaryModel ActiveRecord */
  474. $primaryModel = reset($primaryModels);
  475. if (!$primaryModel instanceof ActiveRecordInterface) {
  476. // when primaryModels are array of arrays (asArray case)
  477. $primaryModel = new $this->modelClass;
  478. }
  479. return $this->asArray()->all($primaryModel->getDb());
  480. }
  481. }