1 | <?php |
||||
2 | |||||
3 | namespace TheCodingMachine\TDBM\Utils; |
||||
4 | |||||
5 | use Doctrine\DBAL\Connection; |
||||
6 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||||
7 | use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
||||
8 | use TheCodingMachine\TDBM\ResultIterator; |
||||
9 | use TheCodingMachine\TDBM\TDBMInvalidArgumentException; |
||||
10 | |||||
11 | /** |
||||
12 | * @internal |
||||
13 | */ |
||||
14 | class ManyToManyRelationshipPathDescriptor |
||||
15 | { |
||||
16 | /** |
||||
17 | * @var string Unquoted identifier of the target table |
||||
18 | */ |
||||
19 | private $targetTable; |
||||
20 | /** |
||||
21 | * @var string Unquoted identifier of the pivot table |
||||
22 | */ |
||||
23 | private $pivotTable; |
||||
24 | /** |
||||
25 | * @var array |
||||
26 | */ |
||||
27 | private $joinForeignKeys; |
||||
28 | /** |
||||
29 | * @var array |
||||
30 | */ |
||||
31 | private $joinLocalKeys; |
||||
32 | /** |
||||
33 | * @var array |
||||
34 | */ |
||||
35 | private $whereKeys; |
||||
36 | /** |
||||
37 | * @var class-string |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
38 | */ |
||||
39 | private $resultIteratorClass; |
||||
40 | |||||
41 | /** |
||||
42 | * @param string[] $joinForeignKeys |
||||
43 | * @param string[] $joinLocalKeys |
||||
44 | * @param string[] $whereKeys |
||||
45 | * @param class-string $resultIteratorClass |
||||
0 ignored issues
–
show
|
|||||
46 | */ |
||||
47 | public function __construct(string $targetTable, string $pivotTable, array $joinForeignKeys, array $joinLocalKeys, array $whereKeys, string $resultIteratorClass) |
||||
48 | { |
||||
49 | assert(is_a($resultIteratorClass, ResultIterator::class, true), new TDBMInvalidArgumentException('$resultIteratorClass should be a `'. ResultIterator::class. '`. `' . $resultIteratorClass . '` provided.')); |
||||
50 | $this->targetTable = $targetTable; |
||||
51 | $this->pivotTable = $pivotTable; |
||||
52 | $this->joinForeignKeys = $joinForeignKeys; |
||||
53 | $this->joinLocalKeys = $joinLocalKeys; |
||||
54 | $this->whereKeys = $whereKeys; |
||||
55 | $this->resultIteratorClass = $resultIteratorClass; |
||||
56 | } |
||||
57 | |||||
58 | public static function generateModelKey(ForeignKeyConstraint $remoteFk, ForeignKeyConstraint $localFk): string |
||||
59 | { |
||||
60 | return $remoteFk->getLocalTableName() . "." . implode("__", $localFk->getUnquotedLocalColumns()); |
||||
0 ignored issues
–
show
The function
Doctrine\DBAL\Schema\For...nt::getLocalTableName() has been deprecated: Use the table that contains the foreign key as part of its {@see Table::$_fkConstraints} instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
61 | } |
||||
62 | |||||
63 | public function getPivotName(): string |
||||
64 | { |
||||
65 | return $this->pivotTable; |
||||
66 | } |
||||
67 | |||||
68 | public function getTargetName(): string |
||||
69 | { |
||||
70 | return $this->targetTable; |
||||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * Get the `FROM` clause of the query. |
||||
75 | * |
||||
76 | * This may have issue handling namespaced table (e.g. mydb.table) |
||||
77 | */ |
||||
78 | public function getPivotFrom(AbstractPlatform $platform): string |
||||
79 | { |
||||
80 | $mainTable = $this->targetTable; |
||||
81 | $pivotTable = $this->pivotTable; |
||||
82 | |||||
83 | $join = []; |
||||
84 | foreach ($this->joinForeignKeys as $key => $column) { |
||||
85 | $join[] = sprintf( |
||||
86 | '%s.%s = %s.%s', |
||||
87 | $platform->quoteIdentifier($mainTable), |
||||
88 | $platform->quoteIdentifier($column), |
||||
89 | $platform->quoteIdentifier($pivotTable), |
||||
90 | $platform->quoteIdentifier($this->joinLocalKeys[$key]) |
||||
91 | ); |
||||
92 | } |
||||
93 | |||||
94 | return $platform->quoteIdentifier($mainTable) . ' JOIN ' . $platform->quoteIdentifier($pivotTable) . ' ON ' . implode(' AND ', $join); |
||||
95 | } |
||||
96 | |||||
97 | /** |
||||
98 | * Get the `WHERE` clause of the query. |
||||
99 | * |
||||
100 | * This may have issue handling namespaced table (e.g. mydb.table) |
||||
101 | */ |
||||
102 | public function getPivotWhere(AbstractPlatform $platform): string |
||||
103 | { |
||||
104 | $paramList = []; |
||||
105 | foreach ($this->whereKeys as $key => $column) { |
||||
106 | $paramList[] = sprintf('%s.%s = :param%s', $platform->quoteIdentifier($this->pivotTable), $platform->quoteIdentifier($column), $key); |
||||
107 | } |
||||
108 | return implode(' AND ', $paramList); |
||||
109 | } |
||||
110 | |||||
111 | /** |
||||
112 | * @param string[] $primaryKeys |
||||
113 | * @return string[] |
||||
114 | */ |
||||
115 | public function getPivotParams(array $primaryKeys): array |
||||
116 | { |
||||
117 | $params = []; |
||||
118 | foreach ($primaryKeys as $key => $primaryKeyValue) { |
||||
119 | $params['param' . $key] = $primaryKeyValue; |
||||
120 | } |
||||
121 | return $params; |
||||
122 | } |
||||
123 | |||||
124 | /** |
||||
125 | * @return class-string |
||||
0 ignored issues
–
show
|
|||||
126 | */ |
||||
127 | public function getResultIteratorClass(): string |
||||
128 | { |
||||
129 | return $this->resultIteratorClass; |
||||
130 | } |
||||
131 | } |
||||
132 |