Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Models\Concerns; |
| 4 | |
| 5 | use LdapRecord\Models\Relations\HasMany; |
| 6 | use LdapRecord\Models\Relations\HasManyIn; |
| 7 | use LdapRecord\Models\Relations\HasOne; |
| 8 | use LdapRecord\Support\Arr; |
| 9 | |
| 10 | trait HasRelationships |
| 11 | { |
| 12 | /** |
| 13 | * Returns a new has one relationship. |
| 14 | * |
| 15 | * @param mixed $related |
| 16 | * @param string $relationKey |
| 17 | * @param string $foreignKey |
| 18 | * |
| 19 | * @return HasOne |
| 20 | */ |
| 21 | public function hasOne($related, $relationKey, $foreignKey = 'dn') |
| 22 | { |
| 23 | return new HasOne($this->newQuery(), $this, $related, $relationKey, $foreignKey); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Returns a new has many relationship. |
| 28 | * |
| 29 | * @param mixed $related |
| 30 | * @param string $relationKey |
| 31 | * @param string $foreignKey |
| 32 | * |
| 33 | * @return HasMany |
| 34 | */ |
| 35 | public function hasMany($related, $relationKey, $foreignKey = 'dn') |
| 36 | { |
| 37 | return new HasMany($this->newQuery(), $this, $related, $relationKey, $foreignKey, $this->guessRelationshipName()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns a new has many in relationship. |
| 42 | * |
| 43 | * @param mixed $related |
| 44 | * @param string $relationKey |
| 45 | * @param string $foreignKey |
| 46 | * |
| 47 | * @return HasManyIn |
| 48 | */ |
| 49 | public function hasManyIn($related, $relationKey, $foreignKey = 'dn') |
| 50 | { |
| 51 | return new HasManyIn($this->newQuery(), $this, $related, $relationKey, $foreignKey, $this->guessRelationshipName()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the relationships name. |
| 56 | * |
| 57 | * @return string|null |
| 58 | */ |
| 59 | protected function guessRelationshipName() |
| 60 | { |
| 61 | return Arr::last(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3))['function']; |
| 62 | } |
| 63 | } |