PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 412 lines | 172 code | 62 blank | 178 comment | 9 complexity | 0f9778661ecf899f4928753a272bbc87 MD5 | raw file
  1. <?php namespace Illuminate\Database\Eloquent\Relations;
  2. use Illuminate\Database\Eloquent\Model;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Query\Expression;
  5. use Illuminate\Database\Eloquent\Collection;
  6. abstract class HasOneOrMany extends Relation {
  7. /**
  8. * The foreign key of the parent model.
  9. *
  10. * @var string
  11. */
  12. protected $foreignKey;
  13. /**
  14. * The local key of the parent model.
  15. *
  16. * @var string
  17. */
  18. protected $localKey;
  19. /**
  20. * Create a new has one or many relationship instance.
  21. *
  22. * @param \Illuminate\Database\Eloquent\Builder $query
  23. * @param \Illuminate\Database\Eloquent\Model $parent
  24. * @param string $foreignKey
  25. * @param string $localKey
  26. * @return void
  27. */
  28. public function __construct(Builder $query, Model $parent, $foreignKey, $localKey)
  29. {
  30. $this->localKey = $localKey;
  31. $this->foreignKey = $foreignKey;
  32. parent::__construct($query, $parent);
  33. }
  34. /**
  35. * Set the base constraints on the relation query.
  36. *
  37. * @return void
  38. */
  39. public function addConstraints()
  40. {
  41. if (static::$constraints)
  42. {
  43. $this->query->where($this->foreignKey, '=', $this->getParentKey());
  44. $this->query->whereNotNull($this->foreignKey);
  45. }
  46. }
  47. /**
  48. * Add the constraints for a relationship count query.
  49. *
  50. * @param \Illuminate\Database\Eloquent\Builder $query
  51. * @param \Illuminate\Database\Eloquent\Builder $parent
  52. * @return \Illuminate\Database\Eloquent\Builder
  53. */
  54. public function getRelationCountQuery(Builder $query, Builder $parent)
  55. {
  56. if ($parent->getQuery()->from == $query->getQuery()->from)
  57. {
  58. return $this->getRelationCountQueryForSelfRelation($query, $parent);
  59. }
  60. return parent::getRelationCountQuery($query, $parent);
  61. }
  62. /**
  63. * Add the constraints for a relationship count query on the same table.
  64. *
  65. * @param \Illuminate\Database\Eloquent\Builder $query
  66. * @param \Illuminate\Database\Eloquent\Builder $parent
  67. * @return \Illuminate\Database\Eloquent\Builder
  68. */
  69. public function getRelationCountQueryForSelfRelation(Builder $query, Builder $parent)
  70. {
  71. $query->select(new Expression('count(*)'));
  72. $tablePrefix = $this->query->getQuery()->getConnection()->getTablePrefix();
  73. $query->from($query->getModel()->getTable().' as '.$tablePrefix.$hash = $this->getRelationCountHash());
  74. $key = $this->wrap($this->getQualifiedParentKeyName());
  75. return $query->where($hash.'.'.$this->getPlainForeignKey(), '=', new Expression($key));
  76. }
  77. /**
  78. * Get a relationship join table hash.
  79. *
  80. * @return string
  81. */
  82. public function getRelationCountHash()
  83. {
  84. return 'self_'.md5(microtime(true));
  85. }
  86. /**
  87. * Set the constraints for an eager load of the relation.
  88. *
  89. * @param array $models
  90. * @return void
  91. */
  92. public function addEagerConstraints(array $models)
  93. {
  94. $this->query->whereIn($this->foreignKey, $this->getKeys($models, $this->localKey));
  95. }
  96. /**
  97. * Match the eagerly loaded results to their single parents.
  98. *
  99. * @param array $models
  100. * @param \Illuminate\Database\Eloquent\Collection $results
  101. * @param string $relation
  102. * @return array
  103. */
  104. public function matchOne(array $models, Collection $results, $relation)
  105. {
  106. return $this->matchOneOrMany($models, $results, $relation, 'one');
  107. }
  108. /**
  109. * Match the eagerly loaded results to their many parents.
  110. *
  111. * @param array $models
  112. * @param \Illuminate\Database\Eloquent\Collection $results
  113. * @param string $relation
  114. * @return array
  115. */
  116. public function matchMany(array $models, Collection $results, $relation)
  117. {
  118. return $this->matchOneOrMany($models, $results, $relation, 'many');
  119. }
  120. /**
  121. * Match the eagerly loaded results to their many parents.
  122. *
  123. * @param array $models
  124. * @param \Illuminate\Database\Eloquent\Collection $results
  125. * @param string $relation
  126. * @param string $type
  127. * @return array
  128. */
  129. protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
  130. {
  131. $dictionary = $this->buildDictionary($results);
  132. // Once we have the dictionary we can simply spin through the parent models to
  133. // link them up with their children using the keyed dictionary to make the
  134. // matching very convenient and easy work. Then we'll just return them.
  135. foreach ($models as $model)
  136. {
  137. $key = $model->getAttribute($this->localKey);
  138. if (isset($dictionary[$key]))
  139. {
  140. $value = $this->getRelationValue($dictionary, $key, $type);
  141. $model->setRelation($relation, $value);
  142. }
  143. }
  144. return $models;
  145. }
  146. /**
  147. * Get the value of a relationship by one or many type.
  148. *
  149. * @param array $dictionary
  150. * @param string $key
  151. * @param string $type
  152. * @return mixed
  153. */
  154. protected function getRelationValue(array $dictionary, $key, $type)
  155. {
  156. $value = $dictionary[$key];
  157. return $type == 'one' ? reset($value) : $this->related->newCollection($value);
  158. }
  159. /**
  160. * Build model dictionary keyed by the relation's foreign key.
  161. *
  162. * @param \Illuminate\Database\Eloquent\Collection $results
  163. * @return array
  164. */
  165. protected function buildDictionary(Collection $results)
  166. {
  167. $dictionary = array();
  168. $foreign = $this->getPlainForeignKey();
  169. // First we will create a dictionary of models keyed by the foreign key of the
  170. // relationship as this will allow us to quickly access all of the related
  171. // models without having to do nested looping which will be quite slow.
  172. foreach ($results as $result)
  173. {
  174. $dictionary[$result->{$foreign}][] = $result;
  175. }
  176. return $dictionary;
  177. }
  178. /**
  179. * Attach a model instance to the parent model.
  180. *
  181. * @param \Illuminate\Database\Eloquent\Model $model
  182. * @return \Illuminate\Database\Eloquent\Model
  183. */
  184. public function save(Model $model)
  185. {
  186. $model->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
  187. return $model->save() ? $model : false;
  188. }
  189. /**
  190. * Attach an array of models to the parent instance.
  191. *
  192. * @param array $models
  193. * @return array
  194. */
  195. public function saveMany(array $models)
  196. {
  197. array_walk($models, array($this, 'save'));
  198. return $models;
  199. }
  200. /**
  201. * Find a model by its primary key or return new instance of the related model.
  202. *
  203. * @param mixed $id
  204. * @param array $columns
  205. * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
  206. */
  207. public function findOrNew($id, $columns = ['*'])
  208. {
  209. if (is_null($instance = $this->find($id, $columns)))
  210. {
  211. $instance = $this->related->newInstance();
  212. $instance->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
  213. }
  214. return $instance;
  215. }
  216. /**
  217. * Get the first related model record matching the attributes or instantiate it.
  218. *
  219. * @param array $attributes
  220. * @return \Illuminate\Database\Eloquent\Model
  221. */
  222. public function firstOrNew(array $attributes)
  223. {
  224. if (is_null($instance = $this->where($attributes)->first()))
  225. {
  226. $instance = $this->related->newInstance($attributes);
  227. $instance->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
  228. }
  229. return $instance;
  230. }
  231. /**
  232. * Get the first related record matching the attributes or create it.
  233. *
  234. * @param array $attributes
  235. * @return \Illuminate\Database\Eloquent\Model
  236. */
  237. public function firstOrCreate(array $attributes)
  238. {
  239. if (is_null($instance = $this->where($attributes)->first()))
  240. {
  241. $instance = $this->create($attributes);
  242. }
  243. return $instance;
  244. }
  245. /**
  246. * Create or update a related record matching the attributes, and fill it with values.
  247. *
  248. * @param array $attributes
  249. * @param array $values
  250. * @return \Illuminate\Database\Eloquent\Model
  251. */
  252. public function updateOrCreate(array $attributes, array $values = [])
  253. {
  254. $instance = $this->firstOrNew($attributes);
  255. $instance->fill($values);
  256. $instance->save();
  257. return $instance;
  258. }
  259. /**
  260. * Create a new instance of the related model.
  261. *
  262. * @param array $attributes
  263. * @return \Illuminate\Database\Eloquent\Model
  264. */
  265. public function create(array $attributes)
  266. {
  267. // Here we will set the raw attributes to avoid hitting the "fill" method so
  268. // that we do not have to worry about a mass accessor rules blocking sets
  269. // on the models. Otherwise, some of these attributes will not get set.
  270. $instance = $this->related->newInstance($attributes);
  271. $instance->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
  272. $instance->save();
  273. return $instance;
  274. }
  275. /**
  276. * Create an array of new instances of the related model.
  277. *
  278. * @param array $records
  279. * @return array
  280. */
  281. public function createMany(array $records)
  282. {
  283. $instances = array();
  284. foreach ($records as $record)
  285. {
  286. $instances[] = $this->create($record);
  287. }
  288. return $instances;
  289. }
  290. /**
  291. * Perform an update on all the related models.
  292. *
  293. * @param array $attributes
  294. * @return int
  295. */
  296. public function update(array $attributes)
  297. {
  298. if ($this->related->usesTimestamps())
  299. {
  300. $attributes[$this->relatedUpdatedAt()] = $this->related->freshTimestampString();
  301. }
  302. return $this->query->update($attributes);
  303. }
  304. /**
  305. * Get the key for comparing against the parent key in "has" query.
  306. *
  307. * @return string
  308. */
  309. public function getHasCompareKey()
  310. {
  311. return $this->getForeignKey();
  312. }
  313. /**
  314. * Get the foreign key for the relationship.
  315. *
  316. * @return string
  317. */
  318. public function getForeignKey()
  319. {
  320. return $this->foreignKey;
  321. }
  322. /**
  323. * Get the plain foreign key.
  324. *
  325. * @return string
  326. */
  327. public function getPlainForeignKey()
  328. {
  329. $segments = explode('.', $this->getForeignKey());
  330. return $segments[count($segments) - 1];
  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->getTable().'.'.$this->localKey;
  349. }
  350. }