Skip to content

Commit

Permalink
fix inheritance for eager loading and accessors/appends laravel/frame…
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius committed Dec 6, 2024
1 parent 39035ec commit 670949a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use MacropaySolutions\LaravelCrudWizard\Eloquent\CustomRelations\RelationCleverTrait;
use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;

class CleverEloquentBuilder extends Builder
Expand All @@ -17,7 +16,7 @@ class CleverEloquentBuilder extends Builder
*/
public function getRelation($name): Relation
{
$relation = RelationCleverTrait::noConstraints(function () use ($name): Relation {
$relation = Relation::macroNoConstraints(function () use ($name): Relation {
try {
$model = $this->getModel()->newInstance();
/** @var BaseModel $model */
Expand All @@ -43,7 +42,7 @@ public function getRelation($name): Relation
*/
protected function getRelationWithoutConstraints($relation): Relation
{
return RelationCleverTrait::noConstraints(function () use ($relation): Relation {
return Relation::macroNoConstraints(function () use ($relation): Relation {
$model = $this->getModel();
/** @var BaseModel $model */
$model->nowEagerLoadingRelationNameWithNoConstraints = $relation;
Expand All @@ -57,8 +56,8 @@ protected function getRelationWithoutConstraints($relation): Relation
*/
protected function getBelongsToRelation(MorphTo $relation, $type): BelongsTo
{
/** this will work as before, the static property from Relation is overwritten in RelationCleverTrait */
$belongsTo = RelationCleverTrait::noConstraints(function () use ($relation, $type): Relation {
/** this will work as before */
$belongsTo = Relation::macroNoConstraints(function () use ($relation, $type): Relation {
return $this->model->belongsTo(
$type,
$relation->getForeignKeyName(),
Expand Down
20 changes: 0 additions & 20 deletions src/Eloquent/CustomRelations/HasCleverRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ trait HasCleverRelationships
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey): HasOne
{
return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasOne {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand Down Expand Up @@ -69,8 +67,6 @@ protected function newHasOneThrough(
$secondLocalKey,
$this
) extends HasOneThrough {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $farParent,
Expand Down Expand Up @@ -102,8 +98,6 @@ public function __construct(
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey): MorphOne
{
return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphOne {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand All @@ -130,8 +124,6 @@ protected function newBelongsTo(
$relation
): BelongsTo {
return new class ($query, $child, $foreignKey, $ownerKey, $relation, $this) extends BelongsTo {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $child,
Expand Down Expand Up @@ -159,8 +151,6 @@ protected function newMorphTo(
$relation
): MorphTo {
return new class ($query, $parent, $foreignKey, $ownerKey, $type, $relation, $this) extends MorphTo {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand All @@ -183,8 +173,6 @@ public function __construct(
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey): HasMany
{
return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasMany {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand Down Expand Up @@ -221,8 +209,6 @@ protected function newHasManyThrough(
$secondLocalKey,
$this
) extends HasManyThrough {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $farParent,
Expand Down Expand Up @@ -254,8 +240,6 @@ public function __construct(
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey): MorphMany
{
return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphMany {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand Down Expand Up @@ -295,8 +279,6 @@ protected function newBelongsToMany(
$this,
$relationName
) extends BelongsToMany {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand Down Expand Up @@ -352,8 +334,6 @@ protected function newMorphToMany(
$inverse,
$this
) extends MorphToMany {
use RelationCleverTrait;

public function __construct(
Builder $query,
Model $parent,
Expand Down
71 changes: 0 additions & 71 deletions src/Eloquent/CustomRelations/RelationCleverTrait.php

This file was deleted.

63 changes: 63 additions & 0 deletions src/Providers/CrudProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;

class CrudProvider extends ServiceProvider
{
Expand Down Expand Up @@ -58,5 +60,66 @@ function (...$arguments): Model {

return $this;
});

Relation::macro('noConstraintsForRelationName', function (?string $relation = null): ?string {
global $noConstraintsForRelationName;

if (\func_num_args() === 0) {
return $noConstraintsForRelationName ?? null;
}

return $noConstraintsForRelationName = $relation;
});

Relation::macro('macroNoConstraints', function (\Closure $callback, ?string $relationName = null): mixed {
$previous = static::$constraints;
$previousNoConstraintsForRelationName = Relation::noConstraintsForRelationName();

if ('' !== (string)$relationName) {
Relation::noConstraintsForRelationName($relationName);
} else {
static::$constraints = false;
}

try {
return $callback();
} finally {
static::$constraints = $previous;
Relation::noConstraintsForRelationName($previousNoConstraintsForRelationName);
}
});

Relation::macro('setConstraintsStaticFlag', function (BaseModel $model): void {
if (
'' === (string)Relation::noConstraintsForRelationName()
&& '' === (string)$model->nowEagerLoadingRelationNameWithNoConstraints
) {
return;
}

/**
* 1st execution is for ExampleModel $exampleModel on 'rel' relation
* with nowEagerLoadingRelationNameWithNoConstraints = 'rel'
* and with $noConstraintsForRelationName = 'rel'
*/
/* 2nd execution is for UserModel $userModel on 'categories' relation
with nowEagerLoadingRelationNameWithNoConstraints = null
and with $noConstraintsForRelationName = 'rel' */


/* 1st execution is for ExampleModel $exampleModel on 'children' relation
with nowEagerLoadingRelationNameWithNoConstraints = null
and with $noConstraintsForRelationName = 'rel' */
/**
* 2nd execution is for ExampleModel $exampleModel on 'rel' relation
* with nowEagerLoadingRelationNameWithNoConstraints = 'rel'
* and with $noConstraintsForRelationName = 'rel'
*/
/* 3rd execution is for UserModel $userModel on 'categories' relation
with nowEagerLoadingRelationNameWithNoConstraints = null
and with $noConstraintsForRelationName = 'rel' */
static::$constraints =
Relation::noConstraintsForRelationName() !== $model->nowEagerLoadingRelationNameWithNoConstraints;
});
}
}

0 comments on commit 670949a

Please sign in to comment.