1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Relations\Util; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Query\Contract\EntityJoinable; |
6
|
|
|
use Bdf\Prime\Query\JoinClause; |
7
|
|
|
use Bdf\Prime\Query\QueryInterface; |
8
|
|
|
use Bdf\Prime\Query\ReadCommandInterface; |
9
|
|
|
use Bdf\Prime\Relations\AbstractRelation; |
10
|
|
|
use Bdf\Prime\Repository\EntityRepository; |
11
|
|
|
use Bdf\Prime\Repository\RepositoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Configure relation with simple join (one level) relation |
15
|
|
|
* |
16
|
|
|
* @property EntityRepository $distant protected |
17
|
|
|
* @property string $attributeAim protected |
18
|
|
|
*/ |
19
|
|
|
trait SimpleTableJoinRelation |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
2 |
|
public function relationRepository(): RepositoryInterface |
|
|
|
|
25
|
|
|
{ |
26
|
2 |
|
return $this->distant; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
2 |
|
public function link($owner): ReadCommandInterface |
33
|
|
|
{ |
34
|
2 |
|
return $this->query($this->getLocalKeyValue($owner)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
2 |
|
public function join(EntityJoinable $query, string $alias): void |
41
|
|
|
{ |
42
|
|
|
// @fixme ? |
|
|
|
|
43
|
|
|
// if ($alias === null) { |
|
|
|
|
44
|
|
|
// $alias = $this->attributeAim; |
|
|
|
|
45
|
|
|
// } |
|
|
|
|
46
|
|
|
|
47
|
2 |
|
$query->joinEntity( |
48
|
2 |
|
$this->distant->entityName(), |
49
|
|
|
function (JoinClause $clause) use($alias, $query) { $this->buildJoinClause($clause, $query, $alias); }, |
|
|
|
|
50
|
2 |
|
null, |
51
|
2 |
|
$alias |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
// apply relation constraints |
|
|
|
|
55
|
2 |
|
$this->applyConstraints($query, [], '$'.$alias); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
|
public function joinRepositories(EntityJoinable $query, string $alias, $discriminator = null): array |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
1 |
|
$alias => $this->relationRepository() |
|
|
|
|
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @see AbstractRelation::query() |
70
|
|
|
*/ |
71
|
|
|
abstract protected function query($value, $constraints = []): ReadCommandInterface; |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @see AbstractRelation::applyConstraints() |
75
|
|
|
*/ |
76
|
|
|
abstract protected function applyConstraints(ReadCommandInterface $query, $constraints = [], $context = null): ReadCommandInterface; |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @see AbstractRelation::applyContext() |
80
|
|
|
*/ |
81
|
|
|
abstract protected function applyContext(?string $context, $constraints); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Extract local (owner) entity key(s) |
85
|
|
|
* |
86
|
|
|
* If an array of entity is provide, an array of keys must be returned like this (pseudo-code) : |
87
|
|
|
* |
88
|
|
|
* > function getLocalKeyValue (object $entity) -> extractKey($entity) |
89
|
|
|
* > function getLocalKeyValue (object[] $entities) -> $entities->map(getLocalKeyValue(object)) |
90
|
|
|
* |
91
|
|
|
* @param object|object[] $entity The entity, or array of entity |
92
|
|
|
* |
93
|
|
|
* @return mixed Can return any values that defined AbstractRelation::applyWhereKeys() supports |
94
|
|
|
*/ |
95
|
|
|
abstract protected function getLocalKeyValue($entity); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Configure the join clause |
99
|
|
|
* |
100
|
|
|
* Ex: |
101
|
|
|
* |
102
|
|
|
* <code> |
103
|
|
|
* protected function buildJoinClause(JoinClause $clause, QueryInterface $query, $alias) |
104
|
|
|
* { |
105
|
|
|
* // $alias>$key : Check value of attribute $key into distant table ($alias is the alias of the distant table) |
106
|
|
|
* // $this->getLocalAlias($query).$key : Get attribute $key on the local table (getLocalAlias resolve the local table alias) |
107
|
|
|
* // new Attribute(...) : Compare with the attribute value instead of litteral string one |
108
|
|
|
* $clause->on($alias.'>'.$this->distantKey, '=', new Attribute($this->getLocalAlias($query).$this->localKey)); |
109
|
|
|
* } |
110
|
|
|
* </code> |
111
|
|
|
* |
112
|
|
|
* @param JoinClause $clause The clause to build |
113
|
|
|
* @param QueryInterface $query The base query |
114
|
|
|
* @param string $alias The distant table alias |
115
|
|
|
* |
116
|
|
|
* @return void |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
abstract protected function buildJoinClause(JoinClause $clause, $query, $alias); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|