|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\Crudites\Relation; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\BlackBox\Database\ConnectionInterface; |
|
6
|
|
|
use HexMakina\Crudites\CruditesExceptionFactory; |
|
7
|
|
|
|
|
8
|
|
|
class OneToMany extends AbstractRelation |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $pivot_table; |
|
12
|
|
|
protected $pivot_primary; |
|
13
|
|
|
protected $pivot_secondary; |
|
14
|
|
|
|
|
15
|
|
|
public const NAME = 'hasAndBelongsToMany'; |
|
16
|
|
|
|
|
17
|
|
|
public const ACTION_LINK = 'insert'; |
|
18
|
|
|
public const ACTION_UNLINK = 'delete'; |
|
19
|
|
|
|
|
20
|
|
|
public function __debugInfo() |
|
21
|
|
|
{ |
|
22
|
|
|
return array_merge(parent::__debugInfo(), [ |
|
23
|
|
|
'pivot_table' => $this->pivot_table, |
|
24
|
|
|
'pivot_primary' => $this->pivot_primary, |
|
25
|
|
|
'pivot_secondary' => $this->pivot_secondary |
|
26
|
|
|
]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($table, $join, ConnectionInterface $c) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->setConnection($c); |
|
32
|
|
|
$this->pivot_table = $table; |
|
33
|
|
|
$this->propertiesFromJoin($join); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
protected function propertiesFromJoin($join) |
|
38
|
|
|
{ |
|
39
|
|
|
[$this->pivot_primary, $this->pivot_secondary] = array_keys($join); |
|
40
|
|
|
|
|
41
|
|
|
[$this->primary_table, $this->primary_col] = $join[$this->pivot_primary]; |
|
42
|
|
|
[$this->secondary_table, $this->secondary_col] = $join[$this->pivot_secondary]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function link(int $source, $target_ids): array |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->query($source, $target_ids, self::ACTION_LINK); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function unlink(int $source, $target_ids): array |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->query($source, $target_ids, self::ACTION_UNLINK); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getIds(int $source) |
|
56
|
|
|
{ |
|
57
|
|
|
$pivot_table =$this->connection->schema()->table($this->pivot_table); |
|
58
|
|
|
$res = $pivot_table->select([$this->pivot_secondary])->whereEQ($this->pivot_primary, $source); |
|
59
|
|
|
return $res->retCol(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getTargets(int $source): array |
|
63
|
|
|
{ |
|
64
|
|
|
$table =$this->connection->schema()->table($this->secondary_table); |
|
65
|
|
|
$select = $table->select() |
|
66
|
|
|
->join([$this->pivot_table], [[$this->secondary_table, $this->secondary_col, $this->pivot_table, $this->pivot_secondary]], 'INNER') |
|
67
|
|
|
->whereEQ($this->pivot_primary, $source, $this->pivot_table); |
|
68
|
|
|
|
|
69
|
|
|
return $select->retAss(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function query(int $source, array $target_ids, string $method): array |
|
73
|
|
|
{ |
|
74
|
|
|
if($source < 1) { |
|
75
|
|
|
throw new \InvalidArgumentException('MISSING_PARENT_ID'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if($method !== self::ACTION_LINK && $method !== self::ACTION_UNLINK) { |
|
79
|
|
|
throw new \InvalidArgumentException('INVALID_METHOD'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$target_ids = array_unique($target_ids); |
|
83
|
|
|
|
|
84
|
|
|
if(empty($target_ids)){ |
|
85
|
|
|
throw new \InvalidArgumentException('NO_UNIQUE_CHILDREN'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$errors = []; |
|
89
|
|
|
$pivot_table =$this->connection->schema()->table($this->pivot_table); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($target_ids as $target) { |
|
92
|
|
|
|
|
93
|
|
|
$query = call_user_func_array([$pivot_table, $method], |
|
94
|
|
|
[ |
|
95
|
|
|
[ |
|
96
|
|
|
$this->pivot_primary => $source, |
|
97
|
|
|
$this->pivot_secondary => $target |
|
98
|
|
|
] |
|
99
|
|
|
]); |
|
100
|
|
|
$query->prepare(); |
|
101
|
|
|
$query->run(); |
|
102
|
|
|
|
|
103
|
|
|
if (!$query->isSuccess()) { |
|
104
|
|
|
$errors[] = $query->error(); |
|
105
|
|
|
|
|
106
|
|
|
if($query->error()->getCode() !== 1062){ |
|
107
|
|
|
throw CruditesExceptionFactory::make($query); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $errors; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|