Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c767dde

Browse files
committed
DB: Got most view queries working for new stucture
1 parent bc49db7 commit c767dde

File tree

6 files changed

+52
-25
lines changed

6 files changed

+52
-25
lines changed

app/Entities/Models/Bookshelf.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class Bookshelf extends Entity implements CoverImageInterface, HtmlDescriptionIn
1313
use HasFactory;
1414
use HtmlDescriptionTrait;
1515

16-
protected $table = 'bookshelves';
17-
1816
public float $searchFactor = 1.2;
1917

2018
protected $fillable = ['name', 'description', 'image_id'];
@@ -24,10 +22,8 @@ class Bookshelf extends Entity implements CoverImageInterface, HtmlDescriptionIn
2422
/**
2523
* Get the books in this shelf.
2624
* Should not be used directly since does not take into account permissions.
27-
*
28-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
2925
*/
30-
public function books()
26+
public function books(): BelongsToMany
3127
{
3228
return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
3329
->withPivot('order')
@@ -105,13 +101,4 @@ public function appendBook(Book $book)
105101
$maxOrder = $this->books()->max('order');
106102
$this->books()->attach($book->id, ['order' => $maxOrder + 1]);
107103
}
108-
109-
/**
110-
* Get a visible shelf by its slug.
111-
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
112-
*/
113-
public static function getBySlug(string $slug): self
114-
{
115-
return static::visible()->where('slug', '=', $slug)->firstOrFail();
116-
}
117104
}

app/Entities/Models/Entity.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ abstract class Entity extends Model implements
7777
*/
7878
public float $searchFactor = 1.0;
7979

80+
/**
81+
* Set the table to be that used by all entities.
82+
*/
83+
protected $table = 'entities';
84+
85+
/**
86+
* Set global scopes to limit queries down to just the right type of entity.
87+
*/
88+
protected static function booted(): void
89+
{
90+
static::addGlobalScope('entity', new EntityScope());
91+
}
92+
8093
/**
8194
* Get the entities that are visible to the current user.
8295
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace BookStack\Entities\Models;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Scope;
8+
use Illuminate\Database\Query\JoinClause;
9+
10+
class EntityScope implements Scope
11+
{
12+
/**
13+
* Apply the scope to a given Eloquent query builder.
14+
*/
15+
public function apply(Builder $builder, Model $model): void
16+
{
17+
$builder = $builder->where('type', '=', $model->getMorphClass());
18+
if ($model instanceof Page) {
19+
$builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', 'entities.id');
20+
} else {
21+
$builder->leftJoin('entity_container_data', function (JoinClause $join) use ($model) {
22+
$join->on('entity_container_data.entity_id', '=', 'entities.id')
23+
->where('entity_container_data.entity_type', '=', $model->getMorphClass());
24+
});
25+
}
26+
}
27+
}

app/Entities/Queries/ChapterQueries.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ public function usingSlugs(string $bookSlug, string $chapterSlug): Builder
6161

6262
public function visibleForList(): Builder
6363
{
64+
// TODO - Review this is working as expected
6465
return $this->start()
6566
->scopes('visible')
6667
->select(array_merge(static::$listAttributes, ['book_slug' => function ($builder) {
6768
$builder->select('slug')
68-
->from('books')
69-
->whereColumn('books.id', '=', 'chapters.book_id');
69+
->from('entities as books')
70+
->where('type', '=', 'book')
71+
->whereColumn('books.id', '=', 'entities.book_id');
7072
}]));
7173
}
7274
}

app/Entities/Queries/PageQueries.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ public function visibleTemplates(): Builder
112112

113113
protected function mergeBookSlugForSelect(array $columns): array
114114
{
115+
// TODO - Review this is working as expected
115116
return array_merge($columns, ['book_slug' => function ($builder) {
116117
$builder->select('slug')
117-
->from('books')
118-
->whereColumn('books.id', '=', 'pages.book_id');
118+
->from('entities as books')
119+
->where('type', '=', 'book')
120+
->whereColumn('books.id', '=', 'entities.book_id');
119121
}]);
120122
}
121123
}

app/Permissions/PermissionApplicator.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public function checkOwnableUserAccess(Model&OwnableInterface $ownable, string|P
4040
$ownerField = $ownable->getOwnerFieldName();
4141
$ownableFieldVal = $ownable->getAttribute($ownerField);
4242

43-
if (is_null($ownableFieldVal)) {
44-
throw new InvalidArgumentException("{$ownerField} field used but has not been loaded");
45-
}
46-
4743
$isOwner = $user->id === $ownableFieldVal;
4844
$hasRolePermission = $allRolePermission || ($isOwner && $ownRolePermission);
4945

@@ -144,10 +140,10 @@ public function restrictEntityRelationQuery(Builder $query, string $tableName, s
144140
/** @var Builder $query */
145141
$query->where($tableDetails['entityTypeColumn'], '!=', $pageMorphClass)
146142
->orWhereExists(function (QueryBuilder $query) use ($tableDetails, $pageMorphClass) {
147-
$query->select('id')->from('pages')
148-
->whereColumn('pages.id', '=', $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
143+
$query->select('page_id')->from('entity_page_data')
144+
->whereColumn('entity_page_data.page_id', '=', $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
149145
->where($tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'], '=', $pageMorphClass)
150-
->where('pages.draft', '=', false);
146+
->where('entity_page_data.draft', '=', false);
151147
});
152148
});
153149
}

0 commit comments

Comments
 (0)