Skip to content

Commit

Permalink
Merge pull request #93 from kirschbaum-development/laravel-9-php-81
Browse files Browse the repository at this point in the history
Fixes for PHP 8.1 and Laravel 9
  • Loading branch information
luisdalmolin authored Mar 5, 2022
2 parents 11a2521 + 98c60cf commit 704a383
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
13 changes: 7 additions & 6 deletions src/Mixins/RelationshipsExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Kirschbaum\PowerJoins\Mixins;

use Kirschbaum\PowerJoins\PowerJoins;
use Kirschbaum\PowerJoins\StaticCache;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\PowerJoins;

/**
* @method getModel
Expand Down Expand Up @@ -302,7 +303,7 @@ public function getFarParent()
public function getTableOrAliasForModel()
{
return function ($model, $default = null) {
return PowerJoins::$powerJoinAliasesCache[spl_object_id($model)] ?? $default;
return StaticCache::$powerJoinAliasesCache[spl_object_id($model)] ?? $default;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function as(string $alias)
$this->useTableAliasInConditions();

if ($this->model) {
PowerJoins::$powerJoinAliasesCache[spl_object_id($this->model)] = $alias;
StaticCache::$powerJoinAliasesCache[spl_object_id($this->model)] = $alias;
}

return $this;
Expand Down
13 changes: 3 additions & 10 deletions src/PowerJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ trait PowerJoins
*/
private $joinRelationshipCache = [];

/**
* Cache to not join the same relationship twice.
*
* @var array
*/
public static $powerJoinAliasesCache = [];

/**
* Join method map.
*/
Expand All @@ -40,7 +33,7 @@ trait PowerJoins
*/
public function scopeJoinRelationship(Builder $query, $relationName, $callback = null, $joinType = 'join', $useAlias = false, bool $disableExtraConditions = false): void
{
$joinType = PowerJoins::$joinMethodsMap[$joinType] ?? $joinType;
$joinType = static::$joinMethodsMap[$joinType] ?? $joinType;
$useAlias = is_string($callback) ? false : $useAlias;
$callback = $this->formatJoinCallback($relationName, $callback);

Expand Down Expand Up @@ -391,15 +384,15 @@ public function generateAliasForRelationship($relation, $relationName)
*/
public function cachePowerJoinAlias($model, $alias)
{
PowerJoins::$powerJoinAliasesCache[spl_object_id($model)] = $alias;
StaticCache::$powerJoinAliasesCache[spl_object_id($model)] = $alias;
}

/**
* Clear the power join caches.
*/
public function clearPowerJoinCaches()
{
PowerJoins::$powerJoinAliasesCache = [];
StaticCache::$powerJoinAliasesCache = [];

return $this;
}
Expand Down
13 changes: 13 additions & 0 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Kirschbaum\PowerJoins;

class StaticCache
{
/**
* Cache to not join the same relationship twice.
*
* @var array
*/
public static $powerJoinAliasesCache = [];
}

0 comments on commit 704a383

Please sign in to comment.