|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magister\Services\Database\Elegant\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Magister\Services\Database\Elegant\Builder; |
|
6
|
|
|
use Magister\Services\Database\Elegant\Model; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class HasOneOrMany. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class HasOneOrMany extends Relation |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The foreign key of the parent model. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $foreignKey; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The local key of the parent model. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $localKey; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new has one or many relationship instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Magister\Services\Database\Elegant\Builder $query |
|
31
|
|
|
* @param \Magister\Services\Database\Elegant\Model $parent |
|
32
|
|
|
* @param string $foreignKey |
|
33
|
|
|
* @param string $localKey |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Builder $query, Model $parent, $foreignKey, $localKey) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->foreignKey = $foreignKey; |
|
38
|
|
|
$this->localKey = $localKey; |
|
39
|
|
|
|
|
40
|
|
|
parent::__construct($query, $parent); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Set the base constraints on the relation query. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addConstraints() |
|
49
|
|
|
{ |
|
50
|
|
|
if (static::$constraints) { |
|
51
|
|
|
$this->query->where($this->foreignKey, $this->getParentKey()); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the foreign key for the relationship. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getForeignKey() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->foreignKey; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the key value of the parent's local key. |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getParentKey() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->parent->getAttribute($this->localKey); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: