|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/orm package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Orm\Mapper\Relation; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
|
13
|
|
|
use Slick\Common\Utils\Text; |
|
14
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorInterface; |
|
15
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorRegistry; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* AbstractRelation |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\Orm\Mapper\Relation |
|
21
|
|
|
* @author Filipe Silva <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractRelation extends Base |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @readwrite |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $propertyName; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @readwrite |
|
33
|
|
|
* @var EntityDescriptorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $entityDescriptor; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Parent or related entity class name |
|
39
|
|
|
* @readwrite |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $parentEntity; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @readwrite |
|
46
|
|
|
* @var EntityDescriptorInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $parentEntityDescriptor; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @readwrite |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $foreignKey; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the property holding the relation |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getPropertyName() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->propertyName; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Gets the entity descriptor |
|
69
|
|
|
* |
|
70
|
|
|
* @return EntityDescriptorInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getEntityDescriptor() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->entityDescriptor; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets entity descriptor |
|
79
|
|
|
* |
|
80
|
|
|
* @param EntityDescriptorInterface $descriptor |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setEntityDescriptor(EntityDescriptorInterface $descriptor) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->entityDescriptor = $descriptor; |
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Gets parent entity class name |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getParentEntity() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->parentEntity; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Gets the parent or related entity descriptor |
|
101
|
|
|
* |
|
102
|
|
|
* @return EntityDescriptorInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getParentEntityDescriptor() |
|
105
|
|
|
{ |
|
106
|
|
|
if (is_null($this->parentEntityDescriptor)) { |
|
107
|
|
|
$this->setParentEntityDescriptor( |
|
108
|
|
|
EntityDescriptorRegistry::getInstance() |
|
109
|
|
|
->getDescriptorFor($this->parentEntity) |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
return $this->parentEntityDescriptor; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sets parent entity descriptor |
|
117
|
|
|
* |
|
118
|
|
|
* @param EntityDescriptorInterface $parentEntityDescriptor |
|
119
|
|
|
* @return BelongsTo |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setParentEntityDescriptor( |
|
122
|
|
|
EntityDescriptorInterface $parentEntityDescriptor |
|
123
|
|
|
) { |
|
124
|
|
|
$this->parentEntityDescriptor = $parentEntityDescriptor; |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Gets the foreign key field name |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getForeignKey() |
|
134
|
|
|
{ |
|
135
|
|
|
if (is_null($this->foreignKey)) { |
|
136
|
|
|
$name = $this->getParentEntityDescriptor()->getTableName(); |
|
137
|
|
|
$name = Text::singular(strtolower($name)); |
|
138
|
|
|
$this->foreignKey = "{$name}_id"; |
|
139
|
|
|
} |
|
140
|
|
|
return $this->foreignKey; |
|
141
|
|
|
} |
|
142
|
|
|
} |