blob: a8a5cacf2f4cb8b92fb197360c4bc551b71007d3 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Models\Concerns;
4
5use LdapRecord\Models\Relations\HasMany;
6use LdapRecord\Models\Relations\HasManyIn;
7use LdapRecord\Models\Relations\HasOne;
8use LdapRecord\Support\Arr;
9
10trait 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}