Browse Source

Possible fix for issue 206

Possible fix for [issue 206](https://github.com/mevdschee/php-crud-api/issues/206)

soft delete example
```php
'before'=>function(&$cmd, &$db, &$tab, &$id, &$in) { 
	if ($cmd == 'delete') {
		$cmd = 'update'; // change command to update
		foreach($in as $k => $o) {
			$in[$k]->deleted = date('Y-m-d H:i:s', time());
		}				
	}
			
},
'column_authorizer'=>function($cmd, $db ,$tab, $col) { 
	return ( ! in_array($col, array('deleted')));
},
'record_filter'=>function($cmd,$db,$tab) { 
	return array('deleted,is,null');
}
```
Barry Dam 8 years ago
parent
commit
70922c6ec2
1 changed files with 8 additions and 3 deletions
  1. 8
    3
      api.php

+ 8
- 3
api.php View File

@@ -1879,9 +1879,6 @@ class PHP_CRUD_API {
1879 1879
 			// input
1880 1880
 			$multi = $post[0]=='[';
1881 1881
 			$contexts = $this->retrieveInputs($post);
1882
-			if ($before) {
1883
-				$this->applyBeforeHandler($action,$database,$tables[0],$key[0],$before,$contexts);
1884
-			}
1885 1882
 			foreach ($contexts as $context) {
1886 1883
 				$input = $this->filterInputByFields($context,$fields[$tables[0]]);
1887 1884
 
@@ -2635,6 +2632,14 @@ class PHP_CRUD_API {
2635 2632
 			$this->swagger($this->settings);
2636 2633
 		} else {
2637 2634
 			$parameters = $this->getParameters($this->settings);
2635
+			if ($parameters['before']) {
2636
+				if (count($parameters['inputs']) == 0) {
2637
+					for($i=1; $i <= count(explode(',', $parameters['key'][0])); $i++) {
2638
+						$parameters['inputs'][] = new stdClass();
2639
+					}
2640
+				}
2641
+				$this->applyBeforeHandler($parameters['action'], $parameters['database'],$parameters['tables'][0],$parameters['key'][0],$parameters['before'],$parameters['inputs']);
2642
+			}
2638 2643
 			switch($parameters['action']){
2639 2644
 				case 'list': $output = $this->listCommand($parameters); break;
2640 2645
 				case 'read': $output = $this->readCommand($parameters); break;

Loading…
Cancel
Save