Skip to content

Commit 90b4157

Browse files
committed
Entities: Reverted back to data from contents, fixed various issues
1 parent daf0b84 commit 90b4157

22 files changed

+81
-72
lines changed

app/Console/Commands/UpdateUrlCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function handle(Connection $db): int
4545

4646
$columnsToUpdateByTable = [
4747
'attachments' => ['path'],
48-
'entity_page_contents' => ['html', 'text', 'markdown'],
49-
'entity_container_contents' => ['description_html'],
48+
'entity_page_data' => ['html', 'text', 'markdown'],
49+
'entity_container_data' => ['description_html'],
5050
'page_revisions' => ['html', 'text', 'markdown'],
5151
'images' => ['url'],
5252
'settings' => ['value'],

app/Entities/Controllers/PageController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function store(Request $request, string $bookSlug, int $pageId)
120120
$this->validate($request, [
121121
'name' => ['required', 'string', 'max:255'],
122122
]);
123+
123124
$draftPage = $this->queries->findVisibleByIdOrFail($pageId);
124125
$this->checkOwnablePermission(Permission::PageCreate, $draftPage->getParent());
125126

app/Entities/Models/Book.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Book extends Entity implements HasDescriptionInterface, HasCoverInterface,
3434
public float $searchFactor = 1.2;
3535

3636
protected $hidden = ['pivot', 'deleted_at'];
37+
protected $fillable = ['name'];
3738

3839
/**
3940
* Get the url for this book.

app/Entities/Models/Bookshelf.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInter
2020
public float $searchFactor = 1.2;
2121

2222
protected $hidden = ['image_id', 'deleted_at', 'description_html'];
23+
protected $fillable = ['name'];
2324

2425
/**
2526
* Get the books in this shelf.
@@ -28,7 +29,7 @@ class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInter
2829
public function books(): BelongsToMany
2930
{
3031
return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
31-
->select(['entities.*', 'entity_container_contents.*'])
32+
->select(['entities.*', 'entity_container_data.*'])
3233
->withPivot('order')
3334
->orderBy('order', 'asc');
3435
}

app/Entities/Models/Chapter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Chapter extends BookChild implements HasDescriptionInterface, HasDefaultTe
2020

2121
public float $searchFactor = 1.2;
2222
protected $hidden = ['pivot', 'deleted_at', 'description_html'];
23+
protected $fillable = ['name', 'priority'];
2324

2425
/**
2526
* Get the pages that this chapter contains.

app/Entities/Models/ContainerTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function descriptionInfo(): EntityHtmlDescription
1616
}
1717

1818
/**
19-
* @return HasOne<EntityContainerContents, $this>
19+
* @return HasOne<EntityContainerData, $this>
2020
*/
2121
public function relatedData(): HasOne
2222
{
23-
return $this->hasOne(EntityContainerContents::class, 'entity_id', 'id')
23+
return $this->hasOne(EntityContainerData::class, 'entity_id', 'id')
2424
->where('entity_type', '=', $this->getMorphClass());
2525
}
2626
}

app/Entities/Models/Entity.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ abstract class Entity extends Model implements
9595
'name',
9696
'slug',
9797
'book_id',
98+
'chapter_id',
9899
'priority',
99100
'created_at',
100101
'updated_at',
@@ -104,13 +105,12 @@ abstract class Entity extends Model implements
104105
'owned_by',
105106
];
106107

107-
// TODO - Review usages of query-time update or mass insert of entity model data since those will still need to consider the multi-table layout.
108-
109108
/**
110109
* Override the save method to also save the contents for convenience.
111110
*/
112111
public function save(array $options = []): bool
113112
{
113+
/** @var EntityPageData|EntityContainerData $contents */
114114
$contents = $this->relatedData()->firstOrNew();
115115
$contentFields = $this->getContentsAttributes();
116116

@@ -119,10 +119,13 @@ public function save(array $options = []): bool
119119
unset($this->attributes[$key]);
120120
}
121121

122+
$this->setAttribute('type', $this->getMorphClass());
122123
$result = parent::save($options);
123124
$contentsResult = true;
124125

125126
if ($result && $contents->isDirty()) {
127+
$contentsFillData = $contents instanceof EntityPageData ? ['page_id' => $this->id] : ['entity_id' => $this->id, 'entity_type' => $this->getMorphClass()];
128+
$contents->forceFill($contentsFillData);
126129
$contentsResult = $contents->save();
127130
$this->touch();
128131
}
@@ -446,7 +449,7 @@ public function logDescriptor(): string
446449
}
447450

448451
/**
449-
* @return HasOne<EntityContainerContents|EntityPageContents, $this>
452+
* @return HasOne<EntityContainerData|EntityPageData, $this>
450453
*/
451454
abstract public function relatedData(): HasOne;
452455

app/Entities/Models/EntityContainerContents.php renamed to app/Entities/Models/EntityContainerData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @property ?int $image_id
1515
* @property ?int $sort_rule_id
1616
*/
17-
class EntityContainerContents extends Model
17+
class EntityContainerData extends Model
1818
{
1919
public $timestamps = false;
2020
protected $primaryKey = 'entity_id';
@@ -26,7 +26,7 @@ class EntityContainerContents extends Model
2626
public function setKeysForSaveQuery($query): Builder
2727
{
2828
$query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery())
29-
->where('type', '=', $this->entity_type);
29+
->where('entity_type', '=', $this->entity_type);
3030

3131
return $query;
3232
}
@@ -37,7 +37,7 @@ public function setKeysForSaveQuery($query): Builder
3737
protected function setKeysForSelectQuery($query): Builder
3838
{
3939
$query->where($this->getKeyName(), '=', $this->getKeyForSelectQuery())
40-
->where('type', '=', $this->entity_type);
40+
->where('entity_type', '=', $this->entity_type);
4141

4242
return $query;
4343
}

app/Entities/Models/EntityPageContents.php renamed to app/Entities/Models/EntityPageData.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7-
class EntityPageContents extends Model
7+
/**
8+
* @property int $page_id
9+
*/
10+
class EntityPageData extends Model
811
{
912
public $timestamps = false;
1013
protected $primaryKey = 'page_id';

app/Entities/Models/EntityScope.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function apply(Builder $builder, Model $model): void
1616
{
1717
$builder = $builder->where('type', '=', $model->getMorphClass());
1818
if ($model instanceof Page) {
19-
$builder->leftJoin('entity_page_contents', 'entity_page_contents.page_id', '=', 'entities.id');
19+
$builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', 'entities.id');
2020
} else {
21-
$builder->leftJoin('entity_container_contents', function (JoinClause $join) use ($model) {
22-
$join->on('entity_container_contents.entity_id', '=', 'entities.id')
23-
->where('entity_container_contents.entity_type', '=', $model->getMorphClass());
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());
2424
});
2525
}
2626
}

0 commit comments

Comments
 (0)