1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cycle ORM Schema Builder. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Schema\Relation\Traits; |
13
|
|
|
|
14
|
|
|
use Cycle\Schema\Definition\Entity; |
15
|
|
|
use Cycle\Schema\Definition\Field; |
16
|
|
|
use Cycle\Schema\Definition\Map\FieldMap; |
17
|
|
|
use Cycle\Schema\Exception\FieldException; |
18
|
|
|
use Cycle\Schema\Exception\RelationException; |
19
|
|
|
use Cycle\Schema\Relation\OptionSchema; |
20
|
|
|
use Cycle\Schema\Table\Column; |
21
|
|
|
|
22
|
|
|
trait FieldTrait |
23
|
|
|
{ |
24
|
|
|
protected function getField(Entity $entity, int $field): Field |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
return $entity->getFields()->get($this->getOptions()->get($field)); |
|
|
|
|
28
|
|
|
} catch (FieldException $e) { |
29
|
|
|
throw new RelationException( |
30
|
|
|
sprintf( |
31
|
|
|
'Field `%s`.`%s` does not exists, referenced by `%s`', |
32
|
|
|
$entity->getRole(), |
33
|
|
|
$this->getOptions()->get($field), |
|
|
|
|
34
|
|
|
$this->source |
35
|
|
|
), |
36
|
|
|
$e->getCode(), |
37
|
|
|
$e |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function getFields(Entity $entity, int $option): FieldMap |
43
|
|
|
{ |
44
|
|
|
$fields = new FieldMap(); |
45
|
|
|
$keys = (array)$this->getOptions()->get($option); |
46
|
|
|
|
47
|
|
|
foreach ($keys as $key) { |
48
|
|
|
try { |
49
|
|
|
$field = $entity->getFields()->getByColumnName($key); |
50
|
|
|
$name = $entity->getFields()->getKeyByColumnName($key); |
51
|
|
|
|
52
|
|
|
$fields->set($name, $field); |
53
|
|
|
} catch (FieldException $e) { |
54
|
|
|
throw new RelationException( |
55
|
|
|
sprintf( |
56
|
|
|
'Field `%s`.`%s` does not exists, referenced by `%s`.', |
57
|
|
|
$entity->getRole(), |
58
|
|
|
$key, |
59
|
|
|
$this->source |
60
|
|
|
), |
61
|
|
|
$e->getCode(), |
62
|
|
|
$e |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $fields; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function ensureField(Entity $target, string $name, Field $outer, bool $nullable = false): void |
71
|
|
|
{ |
72
|
|
|
// ensure that field will be indexed in memory for fast references |
73
|
|
|
$outer->setReferenced(true); |
74
|
|
|
|
75
|
|
|
if ($target->getFields()->hasColumn($name)) { |
76
|
|
|
// field already exists and defined by the user |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$field = new Field(); |
81
|
|
|
$field->setColumn($name); |
82
|
|
|
$field->setTypecast($outer->getTypecast()); |
83
|
|
|
|
84
|
|
|
switch ($outer->getType()) { |
85
|
|
|
case 'primary': |
86
|
|
|
$field->setType('int'); |
87
|
|
|
break; |
88
|
|
|
case 'bigPrimary': |
89
|
|
|
$field->setType('bigint'); |
90
|
|
|
break; |
91
|
|
|
default: |
92
|
|
|
$field->setType($outer->getType()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($nullable) { |
96
|
|
|
$field->getOptions()->set(Column::OPT_NULLABLE, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$target->getFields()->set($name, $field); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
abstract protected function getOptions(): OptionSchema; |
103
|
|
|
} |
104
|
|
|
|