1 | <?php |
||
2 | |||
3 | namespace HexMakina\Crudites\Relation; |
||
4 | |||
5 | use HexMakina\Crudites\CruditesExceptionFactory; |
||
6 | use HexMakina\Crudites\Relation\OneToMany; |
||
7 | |||
8 | class OneToManyQualified extends OneToMany |
||
9 | { |
||
10 | private $pivot_qualified; // FK to the table storing the qualifiers |
||
11 | |||
12 | private $qualified_table; // table storing the qualifiers (tags) |
||
13 | private $qualified_col; // PK of the table storing the qualifiers |
||
14 | |||
15 | public const NAME = 'hasAndBelongsToManyQualified'; |
||
16 | |||
17 | public function __debugInfo() |
||
18 | { |
||
19 | return array_merge(parent::__debugInfo(), [ |
||
20 | 'pivot_qualified' => $this->pivot_qualified, |
||
21 | 'qualified_table' => $this->qualified_table, |
||
22 | 'qualified_col' => $this->qualified_col |
||
23 | ]); |
||
24 | } |
||
25 | |||
26 | public function __toString() |
||
27 | { |
||
28 | return $this->primary_table . '-hasAndBelongsToManyQualified-' . $this->secondary_table; |
||
29 | } |
||
30 | |||
31 | // what a mess... |
||
32 | protected function propertiesFromJoin($join) |
||
33 | { |
||
34 | $tables = explode('_', $this->pivot_table); |
||
35 | foreach ($join as $pivot_col => $target) { |
||
36 | [$target_table, $target_col] = $target; |
||
37 | |||
38 | if (in_array($target_table, $tables)) { |
||
39 | if (isset($this->primary_table)) { |
||
40 | $this->pivot_secondary = $pivot_col; |
||
41 | $this->secondary_table = $target_table; |
||
42 | $this->secondary_col = $target_col; |
||
43 | } else { |
||
44 | $this->pivot_primary = $pivot_col; |
||
45 | $this->primary_table = $target_table; |
||
46 | $this->primary_col = $target_col; |
||
47 | } |
||
48 | } else { |
||
49 | $this->pivot_qualified = $pivot_col; |
||
50 | $this->qualified_table = $target_table; |
||
51 | $this->qualified_col = $target_col; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public function getTargets(int $source): array |
||
57 | { |
||
58 | $table =$this->connection->schema()->table($this->secondary_table); |
||
59 | $select = $table->select() |
||
60 | ->join([$this->pivot_table], [[$this->secondary_table, $this->secondary_col, $this->pivot_table, $this->pivot_secondary]], 'INNER') |
||
61 | ->whereEQ($this->pivot_primary, $source, $this->pivot_table); |
||
62 | $select->selectAlso([ |
||
63 | $this->pivot_qualified.'s' => ["GROUP_CONCAT(DISTINCT `$this->pivot_qualified` SEPARATOR ', ')"] |
||
64 | ]); |
||
65 | $select->groupBy('id'); |
||
66 | $select->groupBy([$this->pivot_table, $this->pivot_secondary]); |
||
67 | return $select->retAss(); |
||
68 | } |
||
69 | |||
70 | public function link(int $source, $targetWithQualifier): array |
||
71 | { |
||
72 | return $this->query($source, $targetWithQualifier, 'insert'); |
||
73 | } |
||
74 | |||
75 | public function unlink(int $source, $targetWithQualifier): array |
||
76 | { |
||
77 | return $this->query($source, $targetWithQualifier, 'delete'); |
||
78 | } |
||
79 | |||
80 | private function query(int $source, array $targetWithQualifier, string $method): array |
||
81 | { |
||
82 | |||
83 | if($source < 1) { |
||
84 | throw new \InvalidArgumentException('MISSING_PARENT_ID'); |
||
85 | } |
||
86 | |||
87 | if($method !== 'insert' && $method !== 'delete') { |
||
88 | throw new \InvalidArgumentException('INVALID_METHOD'); |
||
89 | } |
||
90 | |||
91 | $errors = []; |
||
92 | |||
93 | try { |
||
94 | vd($targetWithQualifier, $this->pivot_table); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
95 | $pivot_table =$this->connection->schema()->table($this->pivot_table); |
||
96 | |||
97 | foreach ($targetWithQualifier as [$qualified_id, $qualifier_id]) { |
||
98 | |||
99 | if (empty($qualified_id) || empty($qualifier_id)) { |
||
100 | throw new \InvalidArgumentException('MANY_IDS_MISSING_A_QUALIFYING_ID'); |
||
101 | } |
||
102 | $query = call_user_func_array([$pivot_table, $method], |
||
103 | [ |
||
104 | [ |
||
105 | $this->pivot_primary => $source, |
||
106 | $this->pivot_secondary => $qualified_id, |
||
107 | $this->pivot_qualified => $qualifier_id |
||
108 | ] |
||
109 | ]); |
||
110 | $query->run(); |
||
111 | |||
112 | if (!$query->isSuccess()) { |
||
113 | $errors[] = $query->error(); |
||
114 | throw CruditesExceptionFactory::make($query); |
||
115 | } |
||
116 | } |
||
117 | } catch (\Exception $e) { |
||
118 | $errors [] = $e->getMessage(); |
||
119 | } |
||
120 | |||
121 | return $errors; |
||
122 | } |
||
123 | |||
124 | } |
||
125 |