ExecuteQueries.php
3 weeks ago
GuardAttributes.php
3 weeks ago
HasAttributes.php
3 weeks ago
HasDictionary.php
3 weeks ago
HasRelationships.php
3 weeks ago
HasTimestamps.php
3 weeks ago
RelationshipQueries.php
3 weeks ago
HasRelationships.php
249 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Trait defining has_one, has_many, belongs_to, and belongs_to_many relationship builders. |
| 5 | * Infers foreign and local keys from model conventions when omitted. |
| 6 | * Entry point for defining associations on Model subclasses. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Database\Concerns |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Database\Concerns; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\Database\Query\Relations\BelongsTo; |
| 16 | use Kirki\Framework\Database\Query\Relations\BelongsToMany; |
| 17 | use Kirki\Framework\Database\Query\Relations\HasMany; |
| 18 | use Kirki\Framework\Database\Query\Relations\HasOne; |
| 19 | use Kirki\Framework\Supports\Arr; |
| 20 | trait HasRelationships |
| 21 | { |
| 22 | /** |
| 23 | * Define a one-to-one relationship. |
| 24 | * |
| 25 | * Creates a HasOne relation object targeting the given model class. Keys |
| 26 | * default to the parent's foreign key and primary key when omitted. |
| 27 | * |
| 28 | * @param string $related The related model class name |
| 29 | * @param mixed $foreign_key The foreign key name on the related model |
| 30 | * @param mixed $local_key The local key name on the parent model |
| 31 | * |
| 32 | * @return HasOne The relation instance |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | protected function has_one($related, $foreign_key = null, $local_key = null) |
| 37 | { |
| 38 | $instance = new $related(); |
| 39 | $foreign_key = $foreign_key ?? $this->get_foreign_key(); |
| 40 | $local_key = $local_key ?? $this->primary_key; |
| 41 | return new HasOne($instance, $this, $foreign_key, $local_key); |
| 42 | } |
| 43 | /** |
| 44 | * Define a one-to-many relationship. |
| 45 | * |
| 46 | * Creates a HasMany relation object targeting the given model class. Keys |
| 47 | * default similarly to has_one when not explicitly specified. |
| 48 | * |
| 49 | * @param string $related The related model class name |
| 50 | * @param mixed $foreign_key The foreign key name on the related model |
| 51 | * @param mixed $local_key The local key name on the parent model |
| 52 | * |
| 53 | * @return HasMany The relation instance |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | protected function has_many($related, $foreign_key = null, $local_key = null) |
| 58 | { |
| 59 | $instance = new $related(); |
| 60 | $foreign_key = $foreign_key ?? $this->get_foreign_key(); |
| 61 | $local_key = $local_key ?? $this->primary_key; |
| 62 | return new HasMany($instance, $this, $foreign_key, $local_key); |
| 63 | } |
| 64 | /** |
| 65 | * Define an inverse one-to-one or many relationship. |
| 66 | * |
| 67 | * Creates a BelongsTo relation where this model holds the foreign key |
| 68 | * referencing the owner model's primary key (or provided owner key). |
| 69 | * |
| 70 | * @param string $related The related model class name |
| 71 | * @param mixed $foreign_key The foreign key name on this model |
| 72 | * @param mixed $owner_key The referenced key name on the related model |
| 73 | * @param string $relation The name of the relation |
| 74 | * |
| 75 | * @return BelongsTo The relation instance |
| 76 | * |
| 77 | * @since 1.0.0 |
| 78 | */ |
| 79 | protected function belongs_to($related, $foreign_key = null, $owner_key = null, $relation = null) |
| 80 | { |
| 81 | $instance = new $related(); |
| 82 | $foreign_key = $foreign_key ?? $instance->get_foreign_key(); |
| 83 | $owner_key = $owner_key ?? $instance->primary_key; |
| 84 | $relation = $relation ?? $this->guess_belongs_to_relation(); |
| 85 | return new BelongsTo($instance, $this, $foreign_key, $owner_key, $relation); |
| 86 | } |
| 87 | /** |
| 88 | * Guess the name of the relation. |
| 89 | * |
| 90 | * @return string The name of the relation |
| 91 | * |
| 92 | * @since 1.0.0 |
| 93 | */ |
| 94 | protected function guess_belongs_to_relation() |
| 95 | { |
| 96 | [, , $caller] = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
| 97 | return $caller['function']; |
| 98 | } |
| 99 | /** |
| 100 | * Define a many-to-many relationship using a pivot table. |
| 101 | * |
| 102 | * Constructs a BelongsToMany relation with optional pivot table and key |
| 103 | * names. When the pivot table is not given, it is inferred by sorting and |
| 104 | * joining the table names. Parent and related keys default to primary keys. |
| 105 | * |
| 106 | * @param string $related The related model class name |
| 107 | * @param mixed $pivot_table The pivot table name joining the models |
| 108 | * @param mixed $foreign_pivot_key The foreign key for this model on pivot |
| 109 | * @param mixed $related_pivot_key The foreign key for related on pivot |
| 110 | * @param mixed $parent_key The local key on this model |
| 111 | * @param mixed $related_key The key on the related model |
| 112 | * |
| 113 | * @return BelongsToMany The relation instance |
| 114 | * |
| 115 | * @since 1.0.0 |
| 116 | */ |
| 117 | protected function belongs_to_many($related, $pivot_table = null, $foreign_pivot_key = null, $related_pivot_key = null, $parent_key = null, $related_key = null, $relation = null) |
| 118 | { |
| 119 | $instance = new $related(); |
| 120 | $foreign_pivot_key = $foreign_pivot_key ?: $this->get_foreign_key(); |
| 121 | $related_pivot_key = $related_pivot_key ?: $instance->get_foreign_key(); |
| 122 | $parent_key = $parent_key ?: $this->get_primary_key(); |
| 123 | $related_key = $related_key ?: $instance->get_primary_key(); |
| 124 | if ($pivot_table === null) { |
| 125 | $pivot_table = $this->get_pivot_table_name($this->get_table(), $instance->get_table()); |
| 126 | } |
| 127 | $relation = $relation ?? $this->guess_belongs_to_many_relation(); |
| 128 | return new BelongsToMany($instance, $this, $pivot_table, $foreign_pivot_key, $related_pivot_key, $parent_key, $related_key, $relation); |
| 129 | } |
| 130 | /** |
| 131 | * Guess the name of the relation. |
| 132 | * |
| 133 | * @return string The name of the relation |
| 134 | * |
| 135 | * @since 1.0.0 |
| 136 | */ |
| 137 | protected function guess_belongs_to_many_relation() |
| 138 | { |
| 139 | $caller = Arr::first(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) { |
| 140 | return !\in_array($trace['function'], ['belongs_to_many', 'guess_belongs_to_many_relation'], \true); |
| 141 | }); |
| 142 | return $caller['function'] ?? null; |
| 143 | } |
| 144 | /** |
| 145 | * Set a relation on the model. |
| 146 | * |
| 147 | * @param string $relation The relation name |
| 148 | * @param mixed $value The value to set |
| 149 | * |
| 150 | * @return $this The model instance |
| 151 | * |
| 152 | * @since 1.0.0 |
| 153 | */ |
| 154 | public function set_relation($relation, $value) |
| 155 | { |
| 156 | $this->relations[$relation] = $value; |
| 157 | return $this; |
| 158 | } |
| 159 | /** |
| 160 | * Check if a relation is loaded. |
| 161 | * |
| 162 | * @param string $key The relation name |
| 163 | * |
| 164 | * @return bool Whether the relation is loaded |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | public function relation_loaded($key) |
| 169 | { |
| 170 | return \array_key_exists($key, $this->relations); |
| 171 | } |
| 172 | /** |
| 173 | * Get all relations. |
| 174 | * |
| 175 | * @return mixed The relation value |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | */ |
| 179 | public function get_relations() |
| 180 | { |
| 181 | return $this->relations; |
| 182 | } |
| 183 | /** |
| 184 | * Get a relation. |
| 185 | * |
| 186 | * @param string $relation The relation name |
| 187 | * |
| 188 | * @return mixed The relation value |
| 189 | * |
| 190 | * @since 1.0.0 |
| 191 | */ |
| 192 | public function get_relation($relation) |
| 193 | { |
| 194 | return $this->relations[$relation]; |
| 195 | } |
| 196 | /** |
| 197 | * Unset all relations. |
| 198 | * |
| 199 | * @return $this The model instance |
| 200 | * |
| 201 | * @since 1.0.0 |
| 202 | */ |
| 203 | public function unset_relations() |
| 204 | { |
| 205 | $this->relations = []; |
| 206 | return $this; |
| 207 | } |
| 208 | /** |
| 209 | * Unset a relation by relation name. |
| 210 | * |
| 211 | * @param string $relation The relation name |
| 212 | * |
| 213 | * @return $this The model instance |
| 214 | * |
| 215 | * @since 1.0.0 |
| 216 | */ |
| 217 | public function unset_relation($relation) |
| 218 | { |
| 219 | unset($this->relations[$relation]); |
| 220 | return $this; |
| 221 | } |
| 222 | /** |
| 223 | * Create a new model instance without any relations. |
| 224 | * |
| 225 | * @return $this The model instance |
| 226 | * |
| 227 | * @since 1.0.0 |
| 228 | */ |
| 229 | public function without_relations() |
| 230 | { |
| 231 | $model = clone $this; |
| 232 | return $model->unset_relations(); |
| 233 | } |
| 234 | /** |
| 235 | * Create a new model instance without a specific relation. |
| 236 | * |
| 237 | * @param string $relation The relation name |
| 238 | * |
| 239 | * @return $this The model instance |
| 240 | * |
| 241 | * @since 1.0.0 |
| 242 | */ |
| 243 | public function without_relation($relation) |
| 244 | { |
| 245 | $model = clone $this; |
| 246 | return $model->unset_relation($relation); |
| 247 | } |
| 248 | } |
| 249 |