|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\ORM\Schemas\Definitions; |
|
8
|
|
|
|
|
9
|
|
|
use Spiral\Database\Schemas\ColumnInterface; |
|
10
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractColumn; |
|
11
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractTable; |
|
12
|
|
|
use Spiral\ORM\Exceptions\DefinitionException; |
|
13
|
|
|
use Spiral\ORM\Exceptions\SchemaException; |
|
14
|
|
|
use Spiral\ORM\Schemas\SchemaInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Defines set of properties relates to source or target model/record. Including class, location |
|
18
|
|
|
* in database, role name, primary key (if any). |
|
19
|
|
|
* |
|
20
|
|
|
* Attention, at this moment relations do not support multiple primary keys. |
|
21
|
|
|
*/ |
|
22
|
|
|
final class RelationContext |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Record class (source or target). |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $class; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Role name. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $role; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string|null |
|
40
|
|
|
*/ |
|
41
|
|
|
private $database; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $table; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @invisible |
|
50
|
|
|
* |
|
51
|
|
|
* @var AbstractTable |
|
52
|
|
|
*/ |
|
53
|
|
|
private $schema; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param SchemaInterface $schema |
|
57
|
|
|
* @param AbstractTable $table |
|
58
|
|
|
* |
|
59
|
|
|
* @return RelationContext |
|
60
|
|
|
* |
|
61
|
|
|
* @throws SchemaException |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function createContent(SchemaInterface $schema, AbstractTable $table): self |
|
64
|
|
|
{ |
|
65
|
|
|
$context = new self(); |
|
66
|
|
|
$context->class = $schema->getClass(); |
|
67
|
|
|
$context->database = $schema->getDatabase(); |
|
68
|
|
|
$context->table = $schema->getTable(); |
|
69
|
|
|
$context->role = $schema->getRole(); |
|
70
|
|
|
|
|
71
|
|
|
$context->schema = $table; |
|
72
|
|
|
|
|
73
|
|
|
return $context; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getClass(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->class; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getRole(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->role; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return null|string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getDatabase() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->database; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getTable(): string |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->table; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return null|ColumnInterface |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getPrimary() |
|
112
|
|
|
{ |
|
113
|
|
|
$primaryKeys = $this->schema->getPrimaryKeys(); |
|
114
|
|
|
if (count($primaryKeys) == 1) { |
|
115
|
|
|
return $this->getColumn($primaryKeys[0]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/* |
|
119
|
|
|
* Table either have complex primary key or no primary keys. |
|
120
|
|
|
*/ |
|
121
|
|
|
|
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
* |
|
128
|
|
|
* @return AbstractColumn |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getColumn($name): AbstractColumn |
|
131
|
|
|
{ |
|
132
|
|
|
if (!$this->schema->hasColumn($name)) { |
|
133
|
|
|
throw new DefinitionException("Undefined column {$name} in {$this->schema->getName()}"); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return clone $this->schema->column($name); |
|
137
|
|
|
} |
|
138
|
|
|
} |