Skip to content

Commit

Permalink
Correction cache fetch models
Browse files Browse the repository at this point in the history
  • Loading branch information
toyi committed Jan 27, 2022
1 parent 7b0085c commit cb8d9fe
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/HasIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ public static function getIdentifierKey(): string
/**
* @param string $identifier
* @param array $attributes
* @return HasIdentifier|null
* @return mixed
*/
public static function getModelByIdentifier(string $identifier, array $attributes = ['*']): ?static
{
return static::query()->where(static::getIdentifierKey(), '=', $identifier)->first($attributes);
if(!in_array('*', $attributes) && !in_array('id', $attributes)){
$attributes[] = 'id';
}

if (static::identifierHasBeenFetched($identifier)) {
return static::$fetchedIdentifiers[static::class][$identifier];
}

$model = static::query()->where(static::getIdentifierKey(), '=', $identifier)->first($attributes);

if ($model != null) {
static::$fetchedIdentifiers[static::class][$identifier] = $model;
}

return $model;
}

/**
Expand All @@ -30,17 +44,9 @@ public static function getModelByIdentifier(string $identifier, array $attribute
*/
public static function getIdByIdentifier(string $identifier): mixed
{
if (static::identifierHasBeenFetched($identifier)) {
return static::$fetchedIdentifiers[static::class][$identifier];
}

$id = optional(static::getModelByIdentifier($identifier, ['id']))->id;

if ($id != null) {
static::$fetchedIdentifiers[static::class][$identifier] = $id;
}
static::getModelByIdentifier($identifier, ['id']);

return $id;
return optional(static::$fetchedIdentifiers[static::class][$identifier])->id;
}

public static function checkIdentifier(mixed $id, string $identifier): bool
Expand Down

0 comments on commit cb8d9fe

Please sign in to comment.