Skip to content

Commit ee34a99

Browse files
committed
As discussed in #200
1 parent d50eac6 commit ee34a99

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

api.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,15 +1134,9 @@ protected function parseGetParameterArray($get,$name,$characters) {
11341134
return $values;
11351135
}
11361136

1137-
protected function applyAfterCreate($callback,$database,$table,$id) {
1137+
protected function applyAfterWrite($callback,$action,$database,$table,$id,$input) {
11381138
if (is_callable($callback,true)) {
1139-
$fields = $this->findPrimaryKeys($table,$database);
1140-
if (count($fields)!=1) $this->exitWith404('1pk');
1141-
if ($result = $this->db->query("SELECT * FROM ! WHERE ! = ?",array($table, $fields[0], $id))) {
1142-
$object = $this->db->fetchAssoc($result);
1143-
$callback($database,$table,$object);
1144-
$this->db->close($result);
1145-
}
1139+
$callback($action,$database,$table,$id,$input);
11461140
}
11471141
}
11481142

@@ -1481,7 +1475,7 @@ protected function createObject($input,$tables) {
14811475
if (!$result) return null;
14821476
$insertId = $this->db->insertId($result);
14831477
extract($this->settings);
1484-
$this->applyAfterCreate($after_create,$database,$tables[0],$insertId);
1478+
$this->applyAfterWrite($after_write,'create',$database,$tables[0],$insertId,$input);
14851479
return $insertId;
14861480
}
14871481

@@ -1518,6 +1512,7 @@ protected function updateObject($key,$input,$filters,$tables) {
15181512
$this->addWhereFromFilters($filters[$table],$sql,$params);
15191513
$result = $this->db->query($sql,$params);
15201514
if (!$result) return null;
1515+
$this->applyAfterWrite($after_write,'update',$database,$tables[0],$key[0],$input);
15211516
return $this->db->affectedRows($result);
15221517
}
15231518

@@ -1550,6 +1545,7 @@ protected function deleteObject($key,$filters,$tables) {
15501545
$this->addWhereFromFilters($filters[$table],$sql,$params);
15511546
$result = $this->db->query($sql,$params);
15521547
if (!$result) return null;
1548+
$this->applyAfterWrite($after_write,'delete',$database,$tables[0],$key[0],array());
15531549
return $this->db->affectedRows($result);
15541550
}
15551551

@@ -1594,6 +1590,7 @@ protected function incrementObject($key,$input,$filters,$tables,$fields) {
15941590
$this->addWhereFromFilters($filters[$table],$sql,$params);
15951591
$result = $this->db->query($sql,$params);
15961592
if (!$result) return null;
1593+
$this->applyAfterWrite($after_write,'increment',$database,$tables[0],$key[0],$input);
15971594
return $this->db->affectedRows($result);
15981595
}
15991596

@@ -2124,7 +2121,7 @@ public function __construct($config) {
21242121
$input_validator = isset($input_validator)?$input_validator:null;
21252122
$auto_include = isset($auto_include)?$auto_include:null;
21262123
$allow_origin = isset($allow_origin)?$allow_origin:null;
2127-
$after_create = isset($after_create)?$after_create:null;
2124+
$after_write = isset($after_write)?$after_write:null;
21282125

21292126
$db = isset($db)?$db:null;
21302127
$method = isset($method)?$method:null;
@@ -2176,7 +2173,7 @@ public function __construct($config) {
21762173
}
21772174

21782175
$this->db = $db;
2179-
$this->settings = compact('method', 'request', 'get', 'post', 'origin', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator', 'after_create', 'auto_include', 'allow_origin');
2176+
$this->settings = compact('method', 'request', 'get', 'post', 'origin', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator', 'after_write', 'auto_include', 'allow_origin');
21802177
}
21812178

21822179
public static function php_crud_api_transform(&$tables) {

0 commit comments

Comments
 (0)