Skip to content

Commit d50eac6

Browse files
authored
Merge pull request #200 from VyseExhale/patch-1
after_create
2 parents 37f2e0d + b712ebc commit d50eac6

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

api.php

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

1137+
protected function applyAfterCreate($callback,$database,$table,$id) {
1138+
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+
}
1146+
}
1147+
}
1148+
11371149
protected function applyTableAuthorizer($callback,$action,$database,&$tables) {
11381150
if (is_callable($callback,true)) foreach ($tables as $i=>$table) {
11391151
if (!$callback($action,$database,$table)) {
@@ -1467,7 +1479,10 @@ protected function createObject($input,$tables) {
14671479
array_unshift($params, $tables[0]);
14681480
$result = $this->db->query('INSERT INTO ! ('.$keys.') VALUES ('.$values.')',$params);
14691481
if (!$result) return null;
1470-
return $this->db->insertId($result);
1482+
$insertId = $this->db->insertId($result);
1483+
extract($this->settings);
1484+
$this->applyAfterCreate($after_create,$database,$tables[0],$insertId);
1485+
return $insertId;
14711486
}
14721487

14731488
protected function createObjects($inputs,$tables) {
@@ -2109,6 +2124,7 @@ public function __construct($config) {
21092124
$input_validator = isset($input_validator)?$input_validator:null;
21102125
$auto_include = isset($auto_include)?$auto_include:null;
21112126
$allow_origin = isset($allow_origin)?$allow_origin:null;
2127+
$after_create = isset($after_create)?$after_create:null;
21122128

21132129
$db = isset($db)?$db:null;
21142130
$method = isset($method)?$method:null;
@@ -2160,7 +2176,7 @@ public function __construct($config) {
21602176
}
21612177

21622178
$this->db = $db;
2163-
$this->settings = compact('method', 'request', 'get', 'post', 'origin', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator', 'auto_include', 'allow_origin');
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');
21642180
}
21652181

21662182
public static function php_crud_api_transform(&$tables) {

0 commit comments

Comments
 (0)