Following our conversation [210](https://github.com/mevdschee/php-crud-api/pull/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
Hi Maurits,
I needed a callback function that got triggered on a new db entry (Create).
For example I wanted to send an e-mail to a newly registered member.
In this example I have a "members" table with the columns: id , firstName, lastName, email
the yourEmailFunction method will send a welcome message to the newly entered member.
```php
$api = new PHP_CRUD_API(array(
'after_create'=>function($db,$tab,$row) {
if ($tab == 'members')
yourEmailFunction($row['firstName'],$row['lastName'],$row['email']);
}
}
));
$api->executeCommand();
```
Tell me you thoughts... maybe if you like it and willing to accept the pull request I can also create an after_delete and after_update method.