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 | 2 | function (JoinClause $clause) use ($alias, $query) { |
|||
50 | 2 | $this->buildJoinClause($clause, $query, $alias); |
|||
51 | 2 | }, |
|||
52 | 2 | null, |
|||
53 | 2 | $alias |
|||
54 | 2 | ); |
|||
55 | |||||
56 | // apply relation constraints |
||||
57 | 2 | $this->applyConstraints($query, [], '$'.$alias); |
|||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * {@inheritdoc} |
||||
62 | */ |
||||
63 | 1 | public function joinRepositories(EntityJoinable $query, string $alias, $discriminator = null): array |
|||
0 ignored issues
–
show
The parameter
$discriminator is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
64 | { |
||||
65 | 1 | return [ |
|||
66 | 1 | $alias => $this->relationRepository() |
|||
67 | 1 | ]; |
|||
68 | } |
||||
69 | |||||
70 | /** |
||||
71 | * @see AbstractRelation::query() |
||||
72 | */ |
||||
73 | abstract protected function query($value, $constraints = []): ReadCommandInterface; |
||||
74 | |||||
75 | /** |
||||
76 | * @see AbstractRelation::applyConstraints() |
||||
77 | */ |
||||
78 | abstract protected function applyConstraints(ReadCommandInterface $query, $constraints = [], $context = null): ReadCommandInterface; |
||||
79 | |||||
80 | /** |
||||
81 | * @see AbstractRelation::applyContext() |
||||
82 | */ |
||||
83 | abstract protected function applyContext(?string $context, $constraints); |
||||
84 | |||||
85 | /** |
||||
86 | * Extract local (owner) entity key(s) |
||||
87 | * |
||||
88 | * If an array of entity is provide, an array of keys must be returned like this (pseudo-code) : |
||||
89 | * |
||||
90 | * > function getLocalKeyValue (object $entity) -> extractKey($entity) |
||||
91 | * > function getLocalKeyValue (object[] $entities) -> $entities->map(getLocalKeyValue(object)) |
||||
92 | * |
||||
93 | * @param object|object[] $entity The entity, or array of entity |
||||
94 | * |
||||
95 | * @return mixed Can return any values that defined AbstractRelation::applyWhereKeys() supports |
||||
96 | */ |
||||
97 | abstract protected function getLocalKeyValue($entity); |
||||
98 | |||||
99 | /** |
||||
100 | * Configure the join clause |
||||
101 | * |
||||
102 | * Ex: |
||||
103 | * |
||||
104 | * <code> |
||||
105 | * protected function buildJoinClause(JoinClause $clause, QueryInterface $query, $alias) |
||||
106 | * { |
||||
107 | * // $alias>$key : Check value of attribute $key into distant table ($alias is the alias of the distant table) |
||||
108 | * // $this->getLocalAlias($query).$key : Get attribute $key on the local table (getLocalAlias resolve the local table alias) |
||||
109 | * // new Attribute(...) : Compare with the attribute value instead of litteral string one |
||||
110 | * $clause->on($alias.'>'.$this->distantKey, '=', new Attribute($this->getLocalAlias($query).$this->localKey)); |
||||
111 | * } |
||||
112 | * </code> |
||||
113 | * |
||||
114 | * @param JoinClause $clause The clause to build |
||||
115 | * @param QueryInterface $query The base query |
||||
116 | * @param string $alias The distant table alias |
||||
117 | * |
||||
118 | * @return void |
||||
119 | */ |
||||
120 | abstract protected function buildJoinClause(JoinClause $clause, $query, $alias); |
||||
121 | } |
||||
122 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.