HasOneOrMany.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Relations;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Collection;
  6. abstract class HasOneOrMany extends Relation
  7. {
  8. /**
  9. * The foreign key of the parent model.
  10. *
  11. * @var string
  12. */
  13. protected $foreignKey;
  14. /**
  15. * The local key of the parent model.
  16. *
  17. * @var string
  18. */
  19. protected $localKey;
  20. /**
  21. * The count of self joins.
  22. *
  23. * @var int
  24. */
  25. protected static $selfJoinCount = 0;
  26. /**
  27. * Create a new has one or many relationship instance.
  28. *
  29. * @param \Illuminate\Database\Eloquent\Builder $query
  30. * @param \Illuminate\Database\Eloquent\Model $parent
  31. * @param string $foreignKey
  32. * @param string $localKey
  33. * @return void
  34. */
  35. public function __construct(Builder $query, Model $parent, $foreignKey, $localKey)
  36. {
  37. $this->localKey = $localKey;
  38. $this->foreignKey = $foreignKey;
  39. parent::__construct($query, $parent);
  40. }
  41. /**
  42. * Create and return an un-saved instance of the related model.
  43. *
  44. * @param array $attributes
  45. * @return \Illuminate\Database\Eloquent\Model
  46. */
  47. public function make(array $attributes = [])
  48. {
  49. return tap($this->related->newInstance($attributes), function ($instance) {
  50. $this->setForeignAttributesForCreate($instance);
  51. });
  52. }
  53. /**
  54. * Set the base constraints on the relation query.
  55. *
  56. * @return void
  57. */
  58. public function addConstraints()
  59. {
  60. if (static::$constraints) {
  61. $this->query->where($this->foreignKey, '=', $this->getParentKey());
  62. $this->query->whereNotNull($this->foreignKey);
  63. }
  64. }
  65. /**
  66. * Set the constraints for an eager load of the relation.
  67. *
  68. * @param array $models
  69. * @return void
  70. */
  71. public function addEagerConstraints(array $models)
  72. {
  73. $this->query->whereIn(
  74. $this->foreignKey, $this->getKeys($models, $this->localKey)
  75. );
  76. }
  77. /**
  78. * Match the eagerly loaded results to their single parents.
  79. *
  80. * @param array $models
  81. * @param \Illuminate\Database\Eloquent\Collection $results
  82. * @param string $relation
  83. * @return array
  84. */
  85. public function matchOne(array $models, Collection $results, $relation)
  86. {
  87. return $this->matchOneOrMany($models, $results, $relation, 'one');
  88. }
  89. /**
  90. * Match the eagerly loaded results to their many parents.
  91. *
  92. * @param array $models
  93. * @param \Illuminate\Database\Eloquent\Collection $results
  94. * @param string $relation
  95. * @return array
  96. */
  97. public function matchMany(array $models, Collection $results, $relation)
  98. {
  99. return $this->matchOneOrMany($models, $results, $relation, 'many');
  100. }
  101. /**
  102. * Match the eagerly loaded results to their many parents.
  103. *
  104. * @param array $models
  105. * @param \Illuminate\Database\Eloquent\Collection $results
  106. * @param string $relation
  107. * @param string $type
  108. * @return array
  109. */
  110. protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
  111. {
  112. $dictionary = $this->buildDictionary($results);
  113. // Once we have the dictionary we can simply spin through the parent models to
  114. // link them up with their children using the keyed dictionary to make the
  115. // matching very convenient and easy work. Then we'll just return them.
  116. foreach ($models as $model) {
  117. if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) {
  118. $model->setRelation(
  119. $relation, $this->getRelationValue($dictionary, $key, $type)
  120. );
  121. }
  122. }
  123. return $models;
  124. }
  125. /**
  126. * Get the value of a relationship by one or many type.
  127. *
  128. * @param array $dictionary
  129. * @param string $key
  130. * @param string $type
  131. * @return mixed
  132. */
  133. protected function getRelationValue(array $dictionary, $key, $type)
  134. {
  135. $value = $dictionary[$key];
  136. return $type == 'one' ? reset($value) : $this->related->newCollection($value);
  137. }
  138. /**
  139. * Build model dictionary keyed by the relation's foreign key.
  140. *
  141. * @param \Illuminate\Database\Eloquent\Collection $results
  142. * @return array
  143. */
  144. protected function buildDictionary(Collection $results)
  145. {
  146. $foreign = $this->getForeignKeyName();
  147. return $results->mapToDictionary(function ($result) use ($foreign) {
  148. return [$result->{$foreign} => $result];
  149. })->all();
  150. }
  151. /**
  152. * Find a model by its primary key or return new instance of the related model.
  153. *
  154. * @param mixed $id
  155. * @param array $columns
  156. * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
  157. */
  158. public function findOrNew($id, $columns = ['*'])
  159. {
  160. if (is_null($instance = $this->find($id, $columns))) {
  161. $instance = $this->related->newInstance();
  162. $this->setForeignAttributesForCreate($instance);
  163. }
  164. return $instance;
  165. }
  166. /**
  167. * Get the first related model record matching the attributes or instantiate it.
  168. *
  169. * @param array $attributes
  170. * @param array $values
  171. * @return \Illuminate\Database\Eloquent\Model
  172. */
  173. public function firstOrNew(array $attributes, array $values = [])
  174. {
  175. if (is_null($instance = $this->where($attributes)->first())) {
  176. $instance = $this->related->newInstance($attributes + $values);
  177. $this->setForeignAttributesForCreate($instance);
  178. }
  179. return $instance;
  180. }
  181. /**
  182. * Get the first related record matching the attributes or create it.
  183. *
  184. * @param array $attributes
  185. * @param array $values
  186. * @return \Illuminate\Database\Eloquent\Model
  187. */
  188. public function firstOrCreate(array $attributes, array $values = [])
  189. {
  190. if (is_null($instance = $this->where($attributes)->first())) {
  191. $instance = $this->create($attributes + $values);
  192. }
  193. return $instance;
  194. }
  195. /**
  196. * Create or update a related record matching the attributes, and fill it with values.
  197. *
  198. * @param array $attributes
  199. * @param array $values
  200. * @return \Illuminate\Database\Eloquent\Model
  201. */
  202. public function updateOrCreate(array $attributes, array $values = [])
  203. {
  204. return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
  205. $instance->fill($values);
  206. $instance->save();
  207. });
  208. }
  209. /**
  210. * Attach a model instance to the parent model.
  211. *
  212. * @param \Illuminate\Database\Eloquent\Model $model
  213. * @return \Illuminate\Database\Eloquent\Model|false
  214. */
  215. public function save(Model $model)
  216. {
  217. $this->setForeignAttributesForCreate($model);
  218. return $model->save() ? $model : false;
  219. }
  220. /**
  221. * Attach a collection of models to the parent instance.
  222. *
  223. * @param \Traversable|array $models
  224. * @return \Traversable|array
  225. */
  226. public function saveMany($models)
  227. {
  228. foreach ($models as $model) {
  229. $this->save($model);
  230. }
  231. return $models;
  232. }
  233. /**
  234. * Create a new instance of the related model.
  235. *
  236. * @param array $attributes
  237. * @return \Illuminate\Database\Eloquent\Model
  238. */
  239. public function create(array $attributes = [])
  240. {
  241. return tap($this->related->newInstance($attributes), function ($instance) {
  242. $this->setForeignAttributesForCreate($instance);
  243. $instance->save();
  244. });
  245. }
  246. /**
  247. * Create a Collection of new instances of the related model.
  248. *
  249. * @param array $records
  250. * @return \Illuminate\Database\Eloquent\Collection
  251. */
  252. public function createMany(array $records)
  253. {
  254. $instances = $this->related->newCollection();
  255. foreach ($records as $record) {
  256. $instances->push($this->create($record));
  257. }
  258. return $instances;
  259. }
  260. /**
  261. * Set the foreign ID for creating a related model.
  262. *
  263. * @param \Illuminate\Database\Eloquent\Model $model
  264. * @return void
  265. */
  266. protected function setForeignAttributesForCreate(Model $model)
  267. {
  268. $model->setAttribute($this->getForeignKeyName(), $this->getParentKey());
  269. }
  270. /**
  271. * Perform an update on all the related models.
  272. *
  273. * @param array $attributes
  274. * @return int
  275. */
  276. public function update(array $attributes)
  277. {
  278. if ($this->related->usesTimestamps() && ! is_null($this->relatedUpdatedAt())) {
  279. $attributes[$this->relatedUpdatedAt()] = $this->related->freshTimestampString();
  280. }
  281. return $this->query->update($attributes);
  282. }
  283. /**
  284. * Add the constraints for a relationship query.
  285. *
  286. * @param \Illuminate\Database\Eloquent\Builder $query
  287. * @param \Illuminate\Database\Eloquent\Builder $parentQuery
  288. * @param array|mixed $columns
  289. * @return \Illuminate\Database\Eloquent\Builder
  290. */
  291. public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
  292. {
  293. if ($query->getQuery()->from == $parentQuery->getQuery()->from) {
  294. return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
  295. }
  296. return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
  297. }
  298. /**
  299. * Add the constraints for a relationship query on the same table.
  300. *
  301. * @param \Illuminate\Database\Eloquent\Builder $query
  302. * @param \Illuminate\Database\Eloquent\Builder $parentQuery
  303. * @param array|mixed $columns
  304. * @return \Illuminate\Database\Eloquent\Builder
  305. */
  306. public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
  307. {
  308. $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());
  309. $query->getModel()->setTable($hash);
  310. return $query->select($columns)->whereColumn(
  311. $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->getForeignKeyName()
  312. );
  313. }
  314. /**
  315. * Get a relationship join table hash.
  316. *
  317. * @return string
  318. */
  319. public function getRelationCountHash()
  320. {
  321. return 'laravel_reserved_'.static::$selfJoinCount++;
  322. }
  323. /**
  324. * Get the key for comparing against the parent key in "has" query.
  325. *
  326. * @return string
  327. */
  328. public function getExistenceCompareKey()
  329. {
  330. return $this->getQualifiedForeignKeyName();
  331. }
  332. /**
  333. * Get the key value of the parent's local key.
  334. *
  335. * @return mixed
  336. */
  337. public function getParentKey()
  338. {
  339. return $this->parent->getAttribute($this->localKey);
  340. }
  341. /**
  342. * Get the fully qualified parent key name.
  343. *
  344. * @return string
  345. */
  346. public function getQualifiedParentKeyName()
  347. {
  348. return $this->parent->qualifyColumn($this->localKey);
  349. }
  350. /**
  351. * Get the plain foreign key.
  352. *
  353. * @return string
  354. */
  355. public function getForeignKeyName()
  356. {
  357. $segments = explode('.', $this->getQualifiedForeignKeyName());
  358. return end($segments);
  359. }
  360. /**
  361. * Get the foreign key for the relationship.
  362. *
  363. * @return string
  364. */
  365. public function getQualifiedForeignKeyName()
  366. {
  367. return $this->foreignKey;
  368. }
  369. }