Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ActiveRelationTrait.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 $this 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 $this 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. return $this->multiple ? $this->all() : $this->one();
  149. }
  150. /**
  151. * If applicable, populate the query's primary model into the related records' inverse relationship
  152. * @param array $result the array of related records as generated by [[populate()]]
  153. * @since 2.0.9
  154. */
  155. private function addInverseRelations(&$result)
  156. {
  157. if ($this->inverseOf === null) {
  158. return;
  159. }
  160. foreach ($result as $i => $relatedModel) {
  161. if ($relatedModel instanceof ActiveRecordInterface) {
  162. if (!isset($inverseRelation)) {
  163. $inverseRelation = $relatedModel->getRelation($this->inverseOf);
  164. }
  165. $relatedModel->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$this->primaryModel] : $this->primaryModel);
  166. } else {
  167. if (!isset($inverseRelation)) {
  168. $inverseRelation = (new $this->modelClass)->getRelation($this->inverseOf);
  169. }
  170. $result[$i][$this->inverseOf] = $inverseRelation->multiple ? [$this->primaryModel] : $this->primaryModel;
  171. }
  172. }
  173. }
  174. /**
  175. * Finds the related records and populates them into the primary models.
  176. * @param string $name the relation name
  177. * @param array $primaryModels primary models
  178. * @return array the related models
  179. * @throws InvalidConfigException if [[link]] is invalid
  180. */
  181. public function populateRelation($name, &$primaryModels)
  182. {
  183. if (!is_array($this->link)) {
  184. throw new InvalidConfigException('Invalid link: it must be an array of key-value pairs.');
  185. }
  186. if ($this->via instanceof self) {
  187. // via junction table
  188. /* @var $viaQuery ActiveRelationTrait */
  189. $viaQuery = $this->via;
  190. $viaModels = $viaQuery->findJunctionRows($primaryModels);
  191. $this->filterByModels($viaModels);
  192. } elseif (is_array($this->via)) {
  193. // via relation
  194. /* @var $viaQuery ActiveRelationTrait|ActiveQueryTrait */
  195. list($viaName, $viaQuery) = $this->via;
  196. if ($viaQuery->asArray === null) {
  197. // inherit asArray from primary query
  198. $viaQuery->asArray($this->asArray);
  199. }
  200. $viaQuery->primaryModel = null;
  201. $viaModels = $viaQuery->populateRelation($viaName, $primaryModels);
  202. $this->filterByModels($viaModels);
  203. } else {
  204. $this->filterByModels($primaryModels);
  205. }
  206. if (!$this->multiple && count($primaryModels) === 1) {
  207. $model = $this->one();
  208. foreach ($primaryModels as $i => $primaryModel) {
  209. if ($primaryModel instanceof ActiveRecordInterface) {
  210. $primaryModel->populateRelation($name, $model);
  211. } else {
  212. $primaryModels[$i][$name] = $model;
  213. }
  214. if ($this->inverseOf !== null) {
  215. $this->populateInverseRelation($primaryModels, [$model], $name, $this->inverseOf);
  216. }
  217. }
  218. return [$model];
  219. } else {
  220. // https://github.com/yiisoft/yii2/issues/3197
  221. // delay indexing related models after buckets are built
  222. $indexBy = $this->indexBy;
  223. $this->indexBy = null;
  224. $models = $this->all();
  225. if (isset($viaModels, $viaQuery)) {
  226. $buckets = $this->buildBuckets($models, $this->link, $viaModels, $viaQuery->link);
  227. } else {
  228. $buckets = $this->buildBuckets($models, $this->link);
  229. }
  230. $this->indexBy = $indexBy;
  231. if ($this->indexBy !== null && $this->multiple) {
  232. $buckets = $this->indexBuckets($buckets, $this->indexBy);
  233. }
  234. $link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link);
  235. foreach ($primaryModels as $i => $primaryModel) {
  236. if ($this->multiple && count($link) === 1 && is_array($keys = $primaryModel[reset($link)])) {
  237. $value = [];
  238. foreach ($keys as $key) {
  239. $key = $this->normalizeModelKey($key);
  240. if (isset($buckets[$key])) {
  241. if ($this->indexBy !== null) {
  242. // if indexBy is set, array_merge will cause renumbering of numeric array
  243. foreach ($buckets[$key] as $bucketKey => $bucketValue) {
  244. $value[$bucketKey] = $bucketValue;
  245. }
  246. } else {
  247. $value = array_merge($value, $buckets[$key]);
  248. }
  249. }
  250. }
  251. } else {
  252. $key = $this->getModelKey($primaryModel, $link);
  253. $value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? [] : null);
  254. }
  255. if ($primaryModel instanceof ActiveRecordInterface) {
  256. $primaryModel->populateRelation($name, $value);
  257. } else {
  258. $primaryModels[$i][$name] = $value;
  259. }
  260. }
  261. if ($this->inverseOf !== null) {
  262. $this->populateInverseRelation($primaryModels, $models, $name, $this->inverseOf);
  263. }
  264. return $models;
  265. }
  266. }
  267. /**
  268. * @param ActiveRecordInterface[] $primaryModels primary models
  269. * @param ActiveRecordInterface[] $models models
  270. * @param string $primaryName the primary relation name
  271. * @param string $name the relation name
  272. */
  273. private function populateInverseRelation(&$primaryModels, $models, $primaryName, $name)
  274. {
  275. if (empty($models) || empty($primaryModels)) {
  276. return;
  277. }
  278. $model = reset($models);
  279. /* @var $relation ActiveQueryInterface|ActiveQuery */
  280. $relation = $model instanceof ActiveRecordInterface ? $model->getRelation($name) : (new $this->modelClass)->getRelation($name);
  281. if ($relation->multiple) {
  282. $buckets = $this->buildBuckets($primaryModels, $relation->link, null, null, false);
  283. if ($model instanceof ActiveRecordInterface) {
  284. foreach ($models as $model) {
  285. $key = $this->getModelKey($model, $relation->link);
  286. $model->populateRelation($name, isset($buckets[$key]) ? $buckets[$key] : []);
  287. }
  288. } else {
  289. foreach ($primaryModels as $i => $primaryModel) {
  290. if ($this->multiple) {
  291. foreach ($primaryModel as $j => $m) {
  292. $key = $this->getModelKey($m, $relation->link);
  293. $primaryModels[$i][$j][$name] = isset($buckets[$key]) ? $buckets[$key] : [];
  294. }
  295. } elseif (!empty($primaryModel[$primaryName])) {
  296. $key = $this->getModelKey($primaryModel[$primaryName], $relation->link);
  297. $primaryModels[$i][$primaryName][$name] = isset($buckets[$key]) ? $buckets[$key] : [];
  298. }
  299. }
  300. }
  301. } else {
  302. if ($this->multiple) {
  303. foreach ($primaryModels as $i => $primaryModel) {
  304. foreach ($primaryModel[$primaryName] as $j => $m) {
  305. if ($m instanceof ActiveRecordInterface) {
  306. $m->populateRelation($name, $primaryModel);
  307. } else {
  308. $primaryModels[$i][$primaryName][$j][$name] = $primaryModel;
  309. }
  310. }
  311. }
  312. } else {
  313. foreach ($primaryModels as $i => $primaryModel) {
  314. if ($primaryModels[$i][$primaryName] instanceof ActiveRecordInterface) {
  315. $primaryModels[$i][$primaryName]->populateRelation($name, $primaryModel);
  316. } elseif (!empty($primaryModels[$i][$primaryName])) {
  317. $primaryModels[$i][$primaryName][$name] = $primaryModel;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. /**
  324. * @param array $models
  325. * @param array $link
  326. * @param array $viaModels
  327. * @param array $viaLink
  328. * @param boolean $checkMultiple
  329. * @return array
  330. */
  331. private function buildBuckets($models, $link, $viaModels = null, $viaLink = null, $checkMultiple = true)
  332. {
  333. if ($viaModels !== null) {
  334. $map = [];
  335. $viaLinkKeys = array_keys($viaLink);
  336. $linkValues = array_values($link);
  337. foreach ($viaModels as $viaModel) {
  338. $key1 = $this->getModelKey($viaModel, $viaLinkKeys);
  339. $key2 = $this->getModelKey($viaModel, $linkValues);
  340. $map[$key2][$key1] = true;
  341. }
  342. }
  343. $buckets = [];
  344. $linkKeys = array_keys($link);
  345. if (isset($map)) {
  346. foreach ($models as $model) {
  347. $key = $this->getModelKey($model, $linkKeys);
  348. if (isset($map[$key])) {
  349. foreach (array_keys($map[$key]) as $key2) {
  350. $buckets[$key2][] = $model;
  351. }
  352. }
  353. }
  354. } else {
  355. foreach ($models as $model) {
  356. $key = $this->getModelKey($model, $linkKeys);
  357. $buckets[$key][] = $model;
  358. }
  359. }
  360. if ($checkMultiple && !$this->multiple) {
  361. foreach ($buckets as $i => $bucket) {
  362. $buckets[$i] = reset($bucket);
  363. }
  364. }
  365. return $buckets;
  366. }
  367. /**
  368. * Indexes buckets by column name.
  369. *
  370. * @param array $buckets
  371. * @var string|callable $column the name of the column by which the query results should be indexed by.
  372. * This can also be a callable (e.g. anonymous function) that returns the index value based on the given row data.
  373. * @return array
  374. */
  375. private function indexBuckets($buckets, $indexBy)
  376. {
  377. $result = [];
  378. foreach ($buckets as $key => $models) {
  379. $result[$key] = [];
  380. foreach ($models as $model) {
  381. $index = is_string($indexBy) ? $model[$indexBy] : call_user_func($indexBy, $model);
  382. $result[$key][$index] = $model;
  383. }
  384. }
  385. return $result;
  386. }
  387. /**
  388. * @param array $attributes the attributes to prefix
  389. * @return array
  390. */
  391. private function prefixKeyColumns($attributes)
  392. {
  393. if ($this instanceof ActiveQuery && (!empty($this->join) || !empty($this->joinWith))) {
  394. if (empty($this->from)) {
  395. /* @var $modelClass ActiveRecord */
  396. $modelClass = $this->modelClass;
  397. $alias = $modelClass::tableName();
  398. } else {
  399. foreach ($this->from as $alias => $table) {
  400. if (!is_string($alias)) {
  401. $alias = $table;
  402. }
  403. break;
  404. }
  405. }
  406. if (isset($alias)) {
  407. foreach ($attributes as $i => $attribute) {
  408. $attributes[$i] = "$alias.$attribute";
  409. }
  410. }
  411. }
  412. return $attributes;
  413. }
  414. /**
  415. * @param array $models
  416. */
  417. private function filterByModels($models)
  418. {
  419. $attributes = array_keys($this->link);
  420. $attributes = $this->prefixKeyColumns($attributes);
  421. $values = [];
  422. if (count($attributes) === 1) {
  423. // single key
  424. $attribute = reset($this->link);
  425. foreach ($models as $model) {
  426. if (($value = $model[$attribute]) !== null) {
  427. if (is_array($value)) {
  428. $values = array_merge($values, $value);
  429. } else {
  430. $values[] = $value;
  431. }
  432. }
  433. }
  434. } else {
  435. // composite keys
  436. // ensure keys of $this->link are prefixed the same way as $attributes
  437. $prefixedLink = array_combine(
  438. $attributes,
  439. array_values($this->link)
  440. );
  441. foreach ($models as $model) {
  442. $v = [];
  443. foreach ($prefixedLink as $attribute => $link) {
  444. $v[$attribute] = $model[$link];
  445. }
  446. $values[] = $v;
  447. }
  448. }
  449. $this->andWhere(['in', $attributes, array_unique($values, SORT_REGULAR)]);
  450. }
  451. /**
  452. * @param ActiveRecordInterface|array $model
  453. * @param array $attributes
  454. * @return string
  455. */
  456. private function getModelKey($model, $attributes)
  457. {
  458. $key = [];
  459. foreach ($attributes as $attribute) {
  460. $key[] = $this->normalizeModelKey($model[$attribute]);
  461. }
  462. if (count($key) > 1) {
  463. return serialize($key);
  464. }
  465. $key = reset($key);
  466. return is_scalar($key) ? $key : serialize($key);
  467. }
  468. /**
  469. * @param mixed $value raw key value.
  470. * @return string normalized key value.
  471. */
  472. private function normalizeModelKey($value)
  473. {
  474. if (is_object($value) && method_exists($value, '__toString')) {
  475. // ensure matching to special objects, which are convertable to string, for cross-DBMS relations, for example: `|MongoId`
  476. $value = $value->__toString();
  477. }
  478. return $value;
  479. }
  480. /**
  481. * @param array $primaryModels either array of AR instances or arrays
  482. * @return array
  483. */
  484. private function findJunctionRows($primaryModels)
  485. {
  486. if (empty($primaryModels)) {
  487. return [];
  488. }
  489. $this->filterByModels($primaryModels);
  490. /* @var $primaryModel ActiveRecord */
  491. $primaryModel = reset($primaryModels);
  492. if (!$primaryModel instanceof ActiveRecordInterface) {
  493. // when primaryModels are array of arrays (asArray case)
  494. $primaryModel = $this->modelClass;
  495. }
  496. return $this->asArray()->all($primaryModel::getDb());
  497. }
  498. }