funayaki /
cakephp3-soft-delete
| 1 | <?php |
||
| 2 | namespace SoftDelete\ORM; |
||
| 3 | |||
| 4 | use Cake\ORM\Query as CakeQuery; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Soft delete-aware query |
||
| 8 | */ |
||
| 9 | class Query extends CakeQuery |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Overwriting triggerBeforeFind() to let queries not return soft deleted records |
||
| 13 | * |
||
| 14 | * Cake\ORM\Query::triggerBeforeFind() overwritten to add the condition `deleted IS NULL` to every find request |
||
| 15 | * in order to not return soft deleted records. |
||
| 16 | * If the query contains the option `withDeleted`, the condition `deleted IS NULL` is not applied. |
||
| 17 | * |
||
| 18 | * @return void |
||
| 19 | */ |
||
| 20 | public function triggerBeforeFind() |
||
| 21 | { |
||
| 22 | if (!$this->_beforeFindFired && $this->_type === 'select') { |
||
| 23 | parent::triggerBeforeFind(); |
||
| 24 | |||
| 25 | if (method_exists($this, 'getRepository')) { |
||
| 26 | $repository = $this->getRepository(); |
||
| 27 | } else { |
||
| 28 | $repository = $this->repository(); |
||
| 29 | } |
||
| 30 | |||
| 31 | $options = $this->getOptions(); |
||
| 32 | if (method_exists($this, 'getRepository')) { |
||
| 33 | $findWithDeleted = in_array('withDeleted', $options) || $this->getRepository()->findWithDeleted(); |
||
| 34 | } else { |
||
| 35 | $findWithDeleted = in_array('withDeleted', $options) || $this->repository()->findWithDeleted(); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!is_array($options) || !$findWithDeleted) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 39 | $this->andWhere($repository->getActiveExpression()); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 |