MorphOneOrMany.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Relations;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Builder;
  5. abstract class MorphOneOrMany extends HasOneOrMany
  6. {
  7. /**
  8. * The foreign key type for the relationship.
  9. *
  10. * @var string
  11. */
  12. protected $morphType;
  13. /**
  14. * The class name of the parent model.
  15. *
  16. * @var string
  17. */
  18. protected $morphClass;
  19. /**
  20. * Create a new morph one or many relationship instance.
  21. *
  22. * @param \Illuminate\Database\Eloquent\Builder $query
  23. * @param \Illuminate\Database\Eloquent\Model $parent
  24. * @param string $type
  25. * @param string $id
  26. * @param string $localKey
  27. * @return void
  28. */
  29. public function __construct(Builder $query, Model $parent, $type, $id, $localKey)
  30. {
  31. $this->morphType = $type;
  32. $this->morphClass = $parent->getMorphClass();
  33. parent::__construct($query, $parent, $id, $localKey);
  34. }
  35. /**
  36. * Set the base constraints on the relation query.
  37. *
  38. * @return void
  39. */
  40. public function addConstraints()
  41. {
  42. if (static::$constraints) {
  43. parent::addConstraints();
  44. $this->query->where($this->morphType, $this->morphClass);
  45. }
  46. }
  47. /**
  48. * Set the constraints for an eager load of the relation.
  49. *
  50. * @param array $models
  51. * @return void
  52. */
  53. public function addEagerConstraints(array $models)
  54. {
  55. parent::addEagerConstraints($models);
  56. $this->query->where($this->morphType, $this->morphClass);
  57. }
  58. /**
  59. * Attach a model instance to the parent model.
  60. *
  61. * @param \Illuminate\Database\Eloquent\Model $model
  62. * @return \Illuminate\Database\Eloquent\Model
  63. */
  64. public function save(Model $model)
  65. {
  66. $model->setAttribute($this->getMorphType(), $this->morphClass);
  67. return parent::save($model);
  68. }
  69. /**
  70. * Set the foreign ID and type for creating a related model.
  71. *
  72. * @param \Illuminate\Database\Eloquent\Model $model
  73. * @return void
  74. */
  75. protected function setForeignAttributesForCreate(Model $model)
  76. {
  77. $model->{$this->getForeignKeyName()} = $this->getParentKey();
  78. $model->{$this->getMorphType()} = $this->morphClass;
  79. }
  80. /**
  81. * Get the relationship query.
  82. *
  83. * @param \Illuminate\Database\Eloquent\Builder $query
  84. * @param \Illuminate\Database\Eloquent\Builder $parentQuery
  85. * @param array|mixed $columns
  86. * @return \Illuminate\Database\Eloquent\Builder
  87. */
  88. public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
  89. {
  90. return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where(
  91. $this->morphType, $this->morphClass
  92. );
  93. }
  94. /**
  95. * Get the foreign key "type" name.
  96. *
  97. * @return string
  98. */
  99. public function getQualifiedMorphType()
  100. {
  101. return $this->morphType;
  102. }
  103. /**
  104. * Get the plain morph type name without the table.
  105. *
  106. * @return string
  107. */
  108. public function getMorphType()
  109. {
  110. return last(explode('.', $this->morphType));
  111. }
  112. /**
  113. * Get the class name of the parent model.
  114. *
  115. * @return string
  116. */
  117. public function getMorphClass()
  118. {
  119. return $this->morphClass;
  120. }
  121. }