]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/EntityQueryBuilder.php
DB: Aligned entity structure to a common table
[bookstack] / app / Entities / Models / EntityQueryBuilder.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Query\Builder as QueryBuilder;
7
8 class EntityQueryBuilder extends Builder
9 {
10     /**
11      * Create a new Eloquent query builder instance.
12      */
13     public function __construct(QueryBuilder $query)
14     {
15         parent::__construct($query);
16
17         $this->withGlobalScope('entity', new EntityScope());
18     }
19
20     public function withoutGlobalScope($scope): static
21     {
22         // Prevent removal of the entity scope
23         if ($scope === 'entity') {
24             return $this;
25         }
26
27         return parent::withoutGlobalScope($scope);
28     }
29
30     /**
31      * Override the default forceDelete method to add type filter onto the query
32      * since it specifically ignores scopes by default.
33      */
34     public function forceDelete()
35     {
36         return $this->query->where('type', '=', $this->model->getMorphClass())->delete();
37     }
38 }