Skip to content

Commit 6e4a591

Browse files
authored
Merge pull request #905 from natanfelles/beforeDelete
Add Model beforeDelete Event - closes #902
2 parents ddb3123 + a533c2e commit 6e4a591

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

system/Model.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class Model
248248
protected $beforeUpdate = [];
249249
protected $afterUpdate = [];
250250
protected $afterFind = [];
251+
protected $beforeDelete = [];
251252
protected $afterDelete = [];
252253

253254
//--------------------------------------------------------------------
@@ -285,7 +286,7 @@ public function __construct(ConnectionInterface &$db = null, BaseConfig $config
285286
{
286287
$validation = \Config\Services::validation(null, false);
287288
}
288-
289+
289290
$this->validation = $validation;
290291
}
291292

@@ -689,6 +690,8 @@ public function update($id, $data)
689690
*/
690691
public function delete($id, $purge = false)
691692
{
693+
$this->trigger('beforeDelete', ['id' => $id, 'purge' => $purge]);
694+
692695
if ($this->useSoftDeletes && ! $purge)
693696
{
694697
$set[$this->deletedField] = 1;
@@ -735,6 +738,8 @@ public function deleteWhere($key, $value = null, $purge = false)
735738
throw new DatabaseException('You must provided a valid key to deleteWhere.');
736739
}
737740

741+
$this->trigger('beforeDelete', ['key' => $key, 'value' => $value, 'purge' => $purge]);
742+
738743
if ($this->useSoftDeletes && ! $purge)
739744
{
740745
$set[$this->deletedField] = 1;
@@ -1172,7 +1177,7 @@ public function getValidationRules(array $options=[])
11721177
{
11731178
$rules = array_intersect_key($rules, array_flip($options['only']));
11741179
}
1175-
1180+
11761181
return $rules;
11771182
}
11781183

user_guide_src/source/database/model.rst

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -587,25 +587,36 @@ passed to each event:
587587
================ =========================================================================================================
588588
Event $data contents
589589
================ =========================================================================================================
590-
beforeInsert **data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the insert
591-
method, it is first converted to an array.
592-
afterInsert **data** = the original key/value pairs being inserted. **result** = the results of the insert() method
593-
used through the Query Builder.
594-
beforeUpdate **id** = the primary key of the row being updated. **data** = the key/value pairs that are being
595-
inserted. If an object or Entity class is passed to the insert method, it is first converted to an array.
596-
afterUpdate **id** = the primary key of the row being updated. **data** = the original key/value pairs being updated.
597-
**result** = the results of the update() method used through the Query Builder.
598-
afterFind Varies by find* method. See the following:
599-
- find() **id** = the primary key of the row being searched for. **data** = The resulting row of data, or null if
600-
no result found.
601-
- findWhere() **data** = the resulting rows of data, or null if no result found.
602-
- findAll() **data** = the resulting rows of data, or null if no result found. **limit** = the number of rows to find.
603-
**offset** = the number of rows to skip during the search.
604-
- first() **data** = the resulting row found during the search, or null if none found.
605-
afterDelete Varies by delete* method. See the following:
606-
- delete() **id** = primary key of row being deleted. **purge** boolean whether soft-delete rows should be
607-
hard deleted. **result** = the result of the delete() call on the Query Builder. **data** = unused.
608-
- deleteWhere() **key**/**value** = the key/value pair used to search for rows to delete. **purge** boolean whether
609-
soft-delete rows should be hard deleted. **result** = the result of the delete() call on the Query
610-
Builder. **data** = unused.
590+
beforeInsert **data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the
591+
insert method, it is first converted to an array.
592+
afterInsert **data** = the original key/value pairs being inserted.
593+
**result** = the results of the insert() method used through the Query Builder.
594+
beforeUpdate **id** = the primary key of the row being updated.
595+
**data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the
596+
insert method, it is first converted to an array.
597+
afterUpdate **id** = the primary key of the row being updated.
598+
**data** = the original key/value pairs being updated.
599+
**result** = the results of the update() method used through the Query Builder.
600+
afterFind Varies by find* method. See the following:
601+
- find() **id** = the primary key of the row being searched for.
602+
**data** = The resulting row of data, or null if no result found.
603+
- findWhere() **data** = the resulting rows of data, or null if no result found.
604+
- findAll() **data** = the resulting rows of data, or null if no result found.
605+
**limit** = the number of rows to find.
606+
**offset** = the number of rows to skip during the search.
607+
- first() **data** = the resulting row found during the search, or null if none found.
608+
beforeDelete Varies by delete* method. See the following:
609+
- delete() **id** = primary key of row being deleted.
610+
**purge** = boolean whether soft-delete rows should be hard deleted.
611+
- deleteWhere() **key**/**value** = the key/value pair used to search for rows to delete.
612+
**purge** = boolean whether soft-delete rows should be hard deleted.
613+
afterDelete Varies by delete* method. See the following:
614+
- delete() **id** = primary key of row being deleted.
615+
**purge** = boolean whether soft-delete rows should be hard deleted.
616+
**result** = the result of the delete() call on the Query Builder.
617+
**data** = unused.
618+
- deleteWhere() **key**/**value** = the key/value pair used to search for rows to delete.
619+
**purge** boolean whether soft-delete rows should be hard deleted.
620+
**result** = the result of the delete() call on the Query Builder.
621+
**data** = unused.
611622
================ =========================================================================================================

0 commit comments

Comments
 (0)