|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\ORM\Schemas\Relations; |
|
8
|
|
|
|
|
9
|
|
|
use Spiral\ORM\Exceptions\OptionsException; |
|
10
|
|
|
use Spiral\ORM\Helpers\RelationOptions; |
|
11
|
|
|
use Spiral\ORM\ORMInterface; |
|
12
|
|
|
use Spiral\ORM\Schemas\Definitions\RelationDefinition; |
|
13
|
|
|
use Spiral\ORM\Schemas\RelationInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Basic class for SQL specific relations. |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractSchema implements RelationInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Relation type to be stored in packed schema. |
|
22
|
|
|
*/ |
|
23
|
|
|
const RELATION_TYPE = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Options to be packed in schema (not all options are required in runtime). |
|
27
|
|
|
*/ |
|
28
|
|
|
const PACK_OPTIONS = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Most of relations provides ability to specify many different configuration options, such |
|
32
|
|
|
* as key names, pivot table schemas, foreign key request, ability to be nullabe and etc. |
|
33
|
|
|
* |
|
34
|
|
|
* To simple schema definition in real projects we can fill some of this values automatically |
|
35
|
|
|
* based on some "environment" values such as parent/outer record table, role name, primary key |
|
36
|
|
|
* and etc. |
|
37
|
|
|
* |
|
38
|
|
|
* Example: |
|
39
|
|
|
* Record::INNER_KEY => '{outer:role}_{outer:primaryKey}' |
|
40
|
|
|
* |
|
41
|
|
|
* Result: |
|
42
|
|
|
* Outer Record is User with primary key "id" => "user_id" |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
const OPTIONS_TEMPLATE = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var RelationDefinition |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $definition; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Provides ability to define missing relation options based on template. |
|
55
|
|
|
* |
|
56
|
|
|
* @see self::OPTIONS_TEMPLATE |
|
57
|
|
|
* @var RelationOptions |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $options; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param RelationDefinition $definition |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct(RelationDefinition $definition) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->definition = $definition; |
|
67
|
|
|
$this->options = new RelationOptions($definition, static::OPTIONS_TEMPLATE); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getDefinition(): RelationDefinition |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->definition; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function packRelation(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
ORMInterface::R_TYPE => static::RELATION_TYPE, |
|
85
|
|
|
ORMInterface::R_OPTIONS => $this->options->defineMultiple(static::PACK_OPTIONS) |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Define relation configuration option. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $option |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
* |
|
96
|
|
|
* @throws OptionsException |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function option(string $option) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->options->define($option); |
|
101
|
|
|
} |
|
102
|
|
|
} |