Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class Model
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];

//--------------------------------------------------------------------
Expand Down Expand Up @@ -285,7 +286,7 @@ public function __construct(ConnectionInterface &$db = null, BaseConfig $config
{
$validation = \Config\Services::validation(null, false);
}

$this->validation = $validation;
}

Expand Down Expand Up @@ -689,6 +690,8 @@ public function update($id, $data)
*/
public function delete($id, $purge = false)
{
$this->trigger('beforeDelete', ['id' => $id, 'purge' => $purge]);

if ($this->useSoftDeletes && ! $purge)
{
$set[$this->deletedField] = 1;
Expand Down Expand Up @@ -735,6 +738,8 @@ public function deleteWhere($key, $value = null, $purge = false)
throw new DatabaseException('You must provided a valid key to deleteWhere.');
}

$this->trigger('beforeDelete', ['key' => $key, 'value' => $value, 'purge' => $purge]);

if ($this->useSoftDeletes && ! $purge)
{
$set[$this->deletedField] = 1;
Expand Down Expand Up @@ -1172,7 +1177,7 @@ public function getValidationRules(array $options=[])
{
$rules = array_intersect_key($rules, array_flip($options['only']));
}

return $rules;
}

Expand Down
53 changes: 32 additions & 21 deletions user_guide_src/source/database/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -587,25 +587,36 @@ passed to each event:
================ =========================================================================================================
Event $data contents
================ =========================================================================================================
beforeInsert **data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the insert
method, it is first converted to an array.
afterInsert **data** = the original key/value pairs being inserted. **result** = the results of the insert() method
used through the Query Builder.
beforeUpdate **id** = the primary key of the row being updated. **data** = the key/value pairs that are being
inserted. If an object or Entity class is passed to the insert method, it is first converted to an array.
afterUpdate **id** = the primary key of the row being updated. **data** = the original key/value pairs being updated.
**result** = the results of the update() method used through the Query Builder.
afterFind Varies by find* method. See the following:
- find() **id** = the primary key of the row being searched for. **data** = The resulting row of data, or null if
no result found.
- findWhere() **data** = the resulting rows of data, or null if no result found.
- findAll() **data** = the resulting rows of data, or null if no result found. **limit** = the number of rows to find.
**offset** = the number of rows to skip during the search.
- first() **data** = the resulting row found during the search, or null if none found.
afterDelete Varies by delete* method. See the following:
- delete() **id** = primary key of row being deleted. **purge** boolean whether soft-delete rows should be
hard deleted. **result** = the result of the delete() call on the Query Builder. **data** = unused.
- deleteWhere() **key**/**value** = the key/value pair used to search for rows to delete. **purge** boolean whether
soft-delete rows should be hard deleted. **result** = the result of the delete() call on the Query
Builder. **data** = unused.
beforeInsert **data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the
insert method, it is first converted to an array.
afterInsert **data** = the original key/value pairs being inserted.
**result** = the results of the insert() method used through the Query Builder.
beforeUpdate **id** = the primary key of the row being updated.
**data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the
insert method, it is first converted to an array.
afterUpdate **id** = the primary key of the row being updated.
**data** = the original key/value pairs being updated.
**result** = the results of the update() method used through the Query Builder.
afterFind Varies by find* method. See the following:
- find() **id** = the primary key of the row being searched for.
**data** = The resulting row of data, or null if no result found.
- findWhere() **data** = the resulting rows of data, or null if no result found.
- findAll() **data** = the resulting rows of data, or null if no result found.
**limit** = the number of rows to find.
**offset** = the number of rows to skip during the search.
- first() **data** = the resulting row found during the search, or null if none found.
beforeDelete Varies by delete* method. See the following:
- delete() **id** = primary key of row being deleted.
**purge** = boolean whether soft-delete rows should be hard deleted.
- deleteWhere() **key**/**value** = the key/value pair used to search for rows to delete.
**purge** = boolean whether soft-delete rows should be hard deleted.
afterDelete Varies by delete* method. See the following:
- delete() **id** = primary key of row being deleted.
**purge** = boolean whether soft-delete rows should be hard deleted.
**result** = the result of the delete() call on the Query Builder.
**data** = unused.
- deleteWhere() **key**/**value** = the key/value pair used to search for rows to delete.
**purge** boolean whether soft-delete rows should be hard deleted.
**result** = the result of the delete() call on the Query Builder.
**data** = unused.
================ =========================================================================================================