1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\Relations; |
4
|
|
|
|
5
|
|
|
use Nip\Records\Relations\HasMany; |
6
|
|
|
use Nip\Records\Relations\Relation; |
7
|
|
|
use Nip\Records\Traits\AbstractTrait\RecordTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait HasRelationsRecordTrait |
11
|
|
|
* @package Nip\Records\Traits\Relations |
12
|
|
|
* |
13
|
|
|
* @method HasRelationsRecordsTrait getManager |
14
|
|
|
*/ |
15
|
|
|
trait HasRelationsRecordTrait |
16
|
|
|
{ |
17
|
|
|
use RecordTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The loaded relationships for the model. |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $relations = []; |
24
|
|
|
|
25
|
|
|
public function saveRelations() |
26
|
|
|
{ |
27
|
|
|
$relations = $this->getRelations(); |
28
|
|
|
foreach ($relations as $relation) { |
29
|
|
|
/** @var Relation $relation */ |
30
|
|
|
$relation->save(); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function getRelations() |
38
|
|
|
{ |
39
|
|
|
return $this->relations; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Clone the relations records from a sibling |
44
|
|
|
* @param self $from |
45
|
|
|
* @return \Nip\Records\Traits\Relations\HasRelationsRecordTrait |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
public function cloneRelations($from) |
49
|
|
|
{ |
50
|
|
|
return $this->getManager()->cloneRelations($from, $this); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $name |
55
|
|
|
* @param $arguments |
56
|
|
|
* @return bool|\Nip\Records\AbstractModels\Record|\Nip\Records\Collections\Collection |
57
|
|
|
*/ |
58
|
|
|
protected function isCallRelationOperation($name, $arguments = []) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (substr($name, 0, 3) == "get") { |
61
|
|
|
$relation = $this->getRelation(substr($name, 3)); |
62
|
|
|
if ($relation) { |
63
|
|
|
$results = $relation->getResults(); |
64
|
|
|
// RETURN NULL TO DISTINCT FROM FALSE THAT MEANS NO RELATION |
65
|
|
|
return ($results) ? $results : null; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $relationName |
74
|
|
|
* @return Relation|HasMany|null |
75
|
|
|
*/ |
76
|
|
|
public function getRelation($relationName) |
77
|
|
|
{ |
78
|
|
|
if (!$this->hasRelation($relationName)) { |
79
|
|
|
$this->initRelation($relationName); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->relations[$relationName]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $key |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasRelation($key) |
90
|
|
|
{ |
91
|
|
|
return array_key_exists($key, $this->relations); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $relationName |
96
|
|
|
*/ |
97
|
|
|
public function initRelation($relationName) |
98
|
|
|
{ |
99
|
|
|
if (!$this->getManager()->hasRelation($relationName)) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
$this->relations[$relationName] = $this->newRelation($relationName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $relationName |
107
|
|
|
* @return Relation|null |
108
|
|
|
*/ |
109
|
1 |
|
public function newRelation($relationName) |
110
|
|
|
{ |
111
|
1 |
|
$relation = clone $this->getManager()->getRelation($relationName); |
112
|
1 |
|
$relation->setItem($this); |
113
|
|
|
|
114
|
1 |
|
return $relation; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.