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