Skip to content

Commit 13af017

Browse files
authored
Bugfix multiple soft deletes (beforeHandler)
Following our conversation [210](#210) The first issue is in the "applyBeforeHandler": ```php $callback($action,$database,$table,$id,$inputs[$i]); ``` $id shoud be $ids[$i], you've probally missed that one : ```php $callback($action,$database,$table,$ids[$i],$inputs[$i]); ``` The second issue will rise if you do 2 or more (soft)deletes: Like ```php DELETE http://localhost/api.php/categories/1,2 ``` In the second [loop](https://github.com/mevdschee/php-crud-api/blob/master/api.php#L1140-L1144) of the applyBeforeHandler, the $action is overwritten by the first loop.. so the in the second before call, the action is update.. so the 2nd one wil never be deleted first: ```php 'before'=>function(&$cmd, $db, $tab, $id, &$in) { $cmd;//delete // then we do this if ($cmd = 'delete') { $cmd = 'update'; // change command to update $in = (object) array(date('Y-m-d H:i:s', time())); } } ``` In the second call cmd is changed to update by the first call: ```php 'before'=>function(&$cmd, $db, $tab, $id, &$in) { $cmd;//update // so the code below never gets executed if ($cmd = 'delete') { $cmd = 'update'; // change command to update $in = (object) array(date('Y-m-d H:i:s', time())); } } ``` I fixed this by storing the $action in a temp variable $origaction; and reset the $action in every start of the loop ```php protected function applyBeforeHandler(&$action,&$database,&$table,&$ids,&$callback,&$inputs) { if (is_callable($callback,true)) { $max = count($ids)?:count($inputs); $origaction = $action; for ($i=0;$i<$max;$i++) { $action = $origaction; if (!isset($ids[$i])) $ids[$i] = false; if (!isset($inputs[$i])) $inputs[$i] = false; $callback($action,$database,$table,$ids[$i],$inputs[$i]); } } } ``` Then the last error, which I allready pointed out before. The call to the [applyBeforeHandler](https://github.com/mevdschee/php-crud-api/blob/master/api.php#L1888-L1890) should be placed AFTER the [foreach](https://github.com/mevdschee/php-crud-api/blob/master/api.php#L1892-L1901) Right now the [filterInputByFields ](https://github.com/mevdschee/php-crud-api/blob/master/api.php#L1893) filteres out the "delete" column which I just inserted in my "before" statement. We may presume that the backend dev kwows what consequences his action have by changing input, so there's no need to call the applyInputTenancy, applyInputSanitizer, applyInputValidator after the applyBeforeHandler is called. So we need to change [this](https://github.com/mevdschee/php-crud-api/blob/master/api.php#L1888-L1901) to: ```php foreach ($inputs as $k=>$context) { $input = $this->filterInputByFields($context,$fields[$tables[0]]); if ($tenancy_function) $this->applyInputTenancy($tenancy_function,$action,$database,$tables[0],$input,$fields[$tables[0]]); if ($input_sanitizer) $this->applyInputSanitizer($input_sanitizer,$action,$database,$tables[0],$input,$fields[$tables[0]]); if ($input_validator) $this->applyInputValidator($input_validator,$action,$database,$tables[0],$input,$fields[$tables[0]],$context); $this->convertInputs($input,$fields[$tables[0]]); $inputs[$k] = $input; } if ($before) { $this->applyBeforeHandler($action,$database,$tables[0],$key[0],$before,$inputs); } ``` Barry
1 parent 1b3ff0d commit 13af017

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

api.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,12 @@ protected function parseGetParameterArray($get,$name,$characters) {
11371137
protected function applyBeforeHandler(&$action,&$database,&$table,&$ids,&$callback,&$inputs) {
11381138
if (is_callable($callback,true)) {
11391139
$max = count($ids)?:count($inputs);
1140+
$origaction = $action;
11401141
for ($i=0;$i<$max;$i++) {
1142+
$action = $origaction;
11411143
if (!isset($ids[$i])) $ids[$i] = false;
11421144
if (!isset($inputs[$i])) $inputs[$i] = false;
1143-
$callback($action,$database,$table,$id,$inputs[$i]);
1145+
$callback($action,$database,$table,$ids[$i],$inputs[$i]);
11441146
}
11451147
}
11461148
}
@@ -1885,10 +1887,6 @@ protected function getParameters($settings) {
18851887

18861888
// input
18871889
$inputs = $this->retrieveInputs($post);
1888-
if ($before) {
1889-
$this->applyBeforeHandler($action,$database,$tables[0],$key[0],$before,$inputs);
1890-
}
1891-
18921890
foreach ($inputs as $k=>$context) {
18931891
$input = $this->filterInputByFields($context,$fields[$tables[0]]);
18941892

@@ -1900,6 +1898,10 @@ protected function getParameters($settings) {
19001898
$inputs[$k] = $input;
19011899
}
19021900

1901+
if ($before) {
1902+
$this->applyBeforeHandler($action,$database,$tables[0],$key[0],$before,$inputs);
1903+
}
1904+
19031905
return compact('action','database','tables','key','page','filters','fields','orderings','transform','inputs','collect','select','before','after');
19041906
}
19051907

0 commit comments

Comments
 (0)