Skip to content

Commit

Permalink
Merge pull request #778 from owlchester/refactor/cte
Browse files Browse the repository at this point in the history
Refactor nested trees to common table expressions
  • Loading branch information
ilestis authored Dec 2, 2023
2 parents eae5ffd + 6505963 commit 7b5643c
Show file tree
Hide file tree
Showing 57 changed files with 316 additions and 1,728 deletions.
78 changes: 0 additions & 78 deletions app/Console/Commands/GenerateTrees.php

This file was deleted.

1 change: 0 additions & 1 deletion app/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function handle()
return;
}
} catch (Exception) {
//
}
$this->call('key:generate');
$this->call('migrate');
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function crudIndex(Request $request)
$parent = null;
if (request()->has('parent_id')) {
// @phpstan-ignore-next-line
$parentKey = $model->getParentIdName();
$parentKey = $model->getParentKeyName();
$base->where([$model->getTable() . '.' . $parentKey => request()->get('parent_id')]);

$parent = $model->where('id', request()->get('parent_id'))->first();
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Locations/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function index(Campaign $campaign, Location $location)
'location', 'location.entity',
'races', 'families',
])
->filter($filters)
->filteredCharacters()
->paginate();

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Locations/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function index(Campaign $campaign, Location $location)
// @phpstan-ignore-next-line
$this->rows = $location
->descendants()
->select(['id', 'image', 'name', 'type', 'location_id', 'is_private'])
->select(['id', 'name', 'type', 'location_id', 'is_private'])
->sort(request()->only(['o', 'k']), ['name' => 'asc'])
->filter($filters)
->with([
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/EntityAssetResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function toArray($request)
/** @var EntityAsset $asset */
$asset = $this->resource;

$data = $this->entity([
$data = $this->onEntity([
'type_id' => $asset->type_id,
'_file' => $asset->isFile(),
'_link' => $asset->isLink(),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/EntityResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function entity(array $prepared = [])

if ($misc->ancestors) {
$ancestors = [];
foreach ($misc->getAncestors(['id']) as $ancestor) {
foreach ($misc->ancestors as $ancestor) {
$ancestors[] = $ancestor->id;
}
$merged['parents'] = $ancestors;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/InventoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function toArray($request)
{
/** @var Inventory $model */
$model = $this->resource;
return $this->entity([
return $this->onEntity([
'item_id' => $model->item_id,
'name' => $model->name,
'position' => $model->position,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function toArray($request)
{
/** @var Post $model */
$model = $this->resource;
return $this->entity([
return $this->onEntity([
'name' => $model->name,
'visibility_id' => (int) $model->visibility_id->value,
'entry' => $model->entry,
Expand Down
18 changes: 4 additions & 14 deletions app/Models/Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Facades\Mentions;
use App\Facades\Module;
use App\Models\Concerns\Acl;
use App\Models\Concerns\Nested;
use App\Models\Concerns\SortableTrait;
use App\Traits\CampaignTrait;
use App\Traits\ExportableTrait;
Expand All @@ -14,6 +13,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Arr;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

/**
* Class Ability
Expand All @@ -34,10 +34,9 @@ class Ability extends MiscModel
use CampaignTrait;
use ExportableTrait;
use HasFactory;
use Nested;
use HasRecursiveRelationships;
use SoftDeletes;
use SortableTrait
;
use SortableTrait;

/** @var string[] */
protected $fillable = [
Expand Down Expand Up @@ -86,20 +85,11 @@ class Ability extends MiscModel
* Parent ID used for the Node Trait
* @return string
*/
public function getParentIdName()
public function getParentKeyName()
{
return 'ability_id';
}

/**
* Specify parent id attribute mutator
* @param int $value
*/
public function setAbilityIdAttribute($value)
{
$this->setParentIdAttribute($value);
}

/**
* Performance with for datagrids
*/
Expand Down
15 changes: 3 additions & 12 deletions app/Models/AttributeTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace App\Models;

use App\Models\Concerns\Acl;
use App\Models\Concerns\Nested;
use App\Services\Attributes\RandomService;
use App\Traits\CampaignTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

/**
* Class AttributeTemplate
Expand All @@ -23,7 +23,7 @@ class AttributeTemplate extends MiscModel
{
use Acl;
use CampaignTrait;
use Nested;
use HasRecursiveRelationships;
use SoftDeletes;

/**
Expand Down Expand Up @@ -96,20 +96,11 @@ public function attributeTemplates()
* Parent ID field for the Node trait
* @return string
*/
public function getParentIdName()
public function getParentKeyName()
{
return 'attribute_template_id';
}

/**
* Specify parent id attribute mutator
* @param int $value
*/
public function setAttributeTemplateIdAttribute($value)
{
$this->setParentIdAttribute($value);
}

/**
* Performance with for datagrids
*/
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

/**
* Class Calendar
Expand Down Expand Up @@ -41,6 +42,7 @@ class Calendar extends MiscModel
use CampaignTrait;
use ExportableTrait;
use HasFactory;
use HasRecursiveRelationships;
use SoftDeletes;

/** @var string[] */
Expand Down Expand Up @@ -134,7 +136,7 @@ public function datagridSelectFields(): array
return ['calendar_id', 'date'];
}

public function getParentIdName(): string
public function getParentKeyName(): string
{
return 'calendar_id';
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Concerns/HasFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,6 @@ protected function getFilterOption(): FilterOption

protected function filterParent(Builder $query): void
{
$query->where($this->getTable() . '.' . $this->getParentIdName(), $this->filterValue);
$query->where($this->getTable() . '.' . $this->getParentKeyName(), $this->filterValue);
}
}
Loading

0 comments on commit 7b5643c

Please sign in to comment.