PageRenderTime 165ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

https://gitlab.com/puntodos/ean-landings
PHP | 308 lines | 126 code | 46 blank | 136 comment | 8 complexity | 4d0a0852da46720de6aefef268147358 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Relations;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Query\Expression;
  6. use Illuminate\Database\Eloquent\Collection;
  7. class BelongsTo extends Relation
  8. {
  9. /**
  10. * The foreign key of the parent model.
  11. *
  12. * @var string
  13. */
  14. protected $foreignKey;
  15. /**
  16. * The associated key on the parent model.
  17. *
  18. * @var string
  19. */
  20. protected $otherKey;
  21. /**
  22. * The name of the relationship.
  23. *
  24. * @var string
  25. */
  26. protected $relation;
  27. /**
  28. * Create a new belongs to relationship instance.
  29. *
  30. * @param \Illuminate\Database\Eloquent\Builder $query
  31. * @param \Illuminate\Database\Eloquent\Model $parent
  32. * @param string $foreignKey
  33. * @param string $otherKey
  34. * @param string $relation
  35. * @return void
  36. */
  37. public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $relation)
  38. {
  39. $this->otherKey = $otherKey;
  40. $this->relation = $relation;
  41. $this->foreignKey = $foreignKey;
  42. parent::__construct($query, $parent);
  43. }
  44. /**
  45. * Get the results of the relationship.
  46. *
  47. * @return mixed
  48. */
  49. public function getResults()
  50. {
  51. return $this->query->first();
  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. // For belongs to relationships, which are essentially the inverse of has one
  62. // or has many relationships, we need to actually query on the primary key
  63. // of the related models matching on the foreign key that's on a parent.
  64. $table = $this->related->getTable();
  65. $this->query->where($table.'.'.$this->otherKey, '=', $this->parent->{$this->foreignKey});
  66. }
  67. }
  68. /**
  69. * Add the constraints for a relationship count query.
  70. *
  71. * @param \Illuminate\Database\Eloquent\Builder $query
  72. * @param \Illuminate\Database\Eloquent\Builder $parent
  73. * @return \Illuminate\Database\Eloquent\Builder
  74. */
  75. public function getRelationCountQuery(Builder $query, Builder $parent)
  76. {
  77. if ($parent->getQuery()->from == $query->getQuery()->from) {
  78. return $this->getRelationCountQueryForSelfRelation($query, $parent);
  79. }
  80. $query->select(new Expression('count(*)'));
  81. $otherKey = $this->wrap($query->getModel()->getTable().'.'.$this->otherKey);
  82. return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
  83. }
  84. /**
  85. * Add the constraints for a relationship count query on the same table.
  86. *
  87. * @param \Illuminate\Database\Eloquent\Builder $query
  88. * @param \Illuminate\Database\Eloquent\Builder $parent
  89. * @return \Illuminate\Database\Eloquent\Builder
  90. */
  91. public function getRelationCountQueryForSelfRelation(Builder $query, Builder $parent)
  92. {
  93. $query->select(new Expression('count(*)'));
  94. $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());
  95. $query->getModel()->setTable($hash);
  96. $key = $this->wrap($this->getQualifiedForeignKey());
  97. return $query->where($hash.'.'.$query->getModel()->getKeyName(), '=', new Expression($key));
  98. }
  99. /**
  100. * Get a relationship join table hash.
  101. *
  102. * @return string
  103. */
  104. public function getRelationCountHash()
  105. {
  106. return 'self_'.md5(microtime(true));
  107. }
  108. /**
  109. * Set the constraints for an eager load of the relation.
  110. *
  111. * @param array $models
  112. * @return void
  113. */
  114. public function addEagerConstraints(array $models)
  115. {
  116. // We'll grab the primary key name of the related models since it could be set to
  117. // a non-standard name and not "id". We will then construct the constraint for
  118. // our eagerly loading query so it returns the proper models from execution.
  119. $key = $this->related->getTable().'.'.$this->otherKey;
  120. $this->query->whereIn($key, $this->getEagerModelKeys($models));
  121. }
  122. /**
  123. * Gather the keys from an array of related models.
  124. *
  125. * @param array $models
  126. * @return array
  127. */
  128. protected function getEagerModelKeys(array $models)
  129. {
  130. $keys = [];
  131. // First we need to gather all of the keys from the parent models so we know what
  132. // to query for via the eager loading query. We will add them to an array then
  133. // execute a "where in" statement to gather up all of those related records.
  134. foreach ($models as $model) {
  135. if (! is_null($value = $model->{$this->foreignKey})) {
  136. $keys[] = $value;
  137. }
  138. }
  139. // If there are no keys that were not null we will just return an array with 0 in
  140. // it so the query doesn't fail, but will not return any results, which should
  141. // be what this developer is expecting in a case where this happens to them.
  142. if (count($keys) == 0) {
  143. return [0];
  144. }
  145. return array_values(array_unique($keys));
  146. }
  147. /**
  148. * Initialize the relation on a set of models.
  149. *
  150. * @param array $models
  151. * @param string $relation
  152. * @return array
  153. */
  154. public function initRelation(array $models, $relation)
  155. {
  156. foreach ($models as $model) {
  157. $model->setRelation($relation, null);
  158. }
  159. return $models;
  160. }
  161. /**
  162. * Match the eagerly loaded results to their parents.
  163. *
  164. * @param array $models
  165. * @param \Illuminate\Database\Eloquent\Collection $results
  166. * @param string $relation
  167. * @return array
  168. */
  169. public function match(array $models, Collection $results, $relation)
  170. {
  171. $foreign = $this->foreignKey;
  172. $other = $this->otherKey;
  173. // First we will get to build a dictionary of the child models by their primary
  174. // key of the relationship, then we can easily match the children back onto
  175. // the parents using that dictionary and the primary key of the children.
  176. $dictionary = [];
  177. foreach ($results as $result) {
  178. $dictionary[$result->getAttribute($other)] = $result;
  179. }
  180. // Once we have the dictionary constructed, we can loop through all the parents
  181. // and match back onto their children using these keys of the dictionary and
  182. // the primary key of the children to map them onto the correct instances.
  183. foreach ($models as $model) {
  184. if (isset($dictionary[$model->$foreign])) {
  185. $model->setRelation($relation, $dictionary[$model->$foreign]);
  186. }
  187. }
  188. return $models;
  189. }
  190. /**
  191. * Associate the model instance to the given parent.
  192. *
  193. * @param \Illuminate\Database\Eloquent\Model|int $model
  194. * @return \Illuminate\Database\Eloquent\Model
  195. */
  196. public function associate($model)
  197. {
  198. $otherKey = ($model instanceof Model ? $model->getAttribute($this->otherKey) : $model);
  199. $this->parent->setAttribute($this->foreignKey, $otherKey);
  200. if ($model instanceof Model) {
  201. $this->parent->setRelation($this->relation, $model);
  202. }
  203. return $this->parent;
  204. }
  205. /**
  206. * Dissociate previously associated model from the given parent.
  207. *
  208. * @return \Illuminate\Database\Eloquent\Model
  209. */
  210. public function dissociate()
  211. {
  212. $this->parent->setAttribute($this->foreignKey, null);
  213. return $this->parent->setRelation($this->relation, null);
  214. }
  215. /**
  216. * Update the parent model on the relationship.
  217. *
  218. * @param array $attributes
  219. * @return mixed
  220. */
  221. public function update(array $attributes)
  222. {
  223. $instance = $this->getResults();
  224. return $instance->fill($attributes)->save();
  225. }
  226. /**
  227. * Get the foreign key of the relationship.
  228. *
  229. * @return string
  230. */
  231. public function getForeignKey()
  232. {
  233. return $this->foreignKey;
  234. }
  235. /**
  236. * Get the fully qualified foreign key of the relationship.
  237. *
  238. * @return string
  239. */
  240. public function getQualifiedForeignKey()
  241. {
  242. return $this->parent->getTable().'.'.$this->foreignKey;
  243. }
  244. /**
  245. * Get the associated key of the relationship.
  246. *
  247. * @return string
  248. */
  249. public function getOtherKey()
  250. {
  251. return $this->otherKey;
  252. }
  253. /**
  254. * Get the fully qualified associated key of the relationship.
  255. *
  256. * @return string
  257. */
  258. public function getQualifiedOtherKeyName()
  259. {
  260. return $this->related->getTable().'.'.$this->otherKey;
  261. }
  262. }