|
18 | 18 | use yii\web\HttpException; |
19 | 19 | use yii\web\NotFoundHttpException; |
20 | 20 | use function array_diff; |
| 21 | +use function call_user_func; |
21 | 22 | use function count; |
22 | 23 | use function gettype; |
23 | 24 | use function implode; |
@@ -94,12 +95,10 @@ public function attach() |
94 | 95 | throw new HttpException(422, 'Missing ids for create relationship'); |
95 | 96 | } |
96 | 97 | if ($this->idValidateCallback !== null) { |
97 | | - $this->idValidateCallback($this->model, $ids); |
| 98 | + call_user_func($this->idValidateCallback, $this->model, $ids); |
98 | 99 | } else { |
99 | 100 | $this->validateIdType($ids, false); |
100 | 101 | } |
101 | | - |
102 | | - |
103 | 102 | $pkAttribute = $this->resolvePkAttribute($relation); |
104 | 103 | $alreadyRelatedIds = $relation->select($pkAttribute)->column(); |
105 | 104 | $forLink = array_diff($ids, $alreadyRelatedIds); |
@@ -150,7 +149,7 @@ public function delete(bool $unlinkOnly = true):void |
150 | 149 |
|
151 | 150 | $ids = ArrayHelper::getColumn($this->data, 'id'); |
152 | 151 | if ($this->idValidateCallback !== null) { |
153 | | - $this->idValidateCallback($this->model, $ids); |
| 152 | + call_user_func($this->idValidateCallback, $this->model, $ids); |
154 | 153 | } else { |
155 | 154 | $this->validateIdType($ids, false); |
156 | 155 | } |
@@ -203,8 +202,9 @@ public function patch($unlinkOnly = true):void |
203 | 202 | if (empty($ids)) { |
204 | 203 | throw new HttpException(422, 'Missing ids for update relationship'); |
205 | 204 | } |
| 205 | + |
206 | 206 | if ($this->idValidateCallback !== null) { |
207 | | - $this->idValidateCallback($this->model, $ids); |
| 207 | + call_user_func($this->idValidateCallback, $this->model, $ids); |
208 | 208 | } else { |
209 | 209 | $this->validateIdType($ids, false); |
210 | 210 | } |
@@ -257,12 +257,13 @@ protected function patchSingle(ActiveQueryInterface $relation, bool $unlinkOnly) |
257 | 257 | $newRelationId = $this->data['id'] ?? null; |
258 | 258 |
|
259 | 259 | if ($this->idValidateCallback !== null) { |
260 | | - $this->idValidateCallback($this->model, [$newRelationId]); |
| 260 | + call_user_func($this->idValidateCallback, $this->model, [$newRelationId]); |
261 | 261 | } else { |
262 | 262 | $this->validateIdType([$newRelationId], true); |
263 | 263 | } |
264 | 264 |
|
265 | 265 | $relatedModel = $relation->one(); |
| 266 | + $alreadyRelatedIds = $relatedModel ? $relatedModel->getPrimaryKey(): []; |
266 | 267 | if (!$newRelationId && $relatedModel) { |
267 | 268 | $this->model->unlink($this->relationName, $relatedModel, !$unlinkOnly); |
268 | 269 | Yii::$app->response->setStatusCode(204); |
|
0 commit comments