[CORE][DB] Add DB::refetch, which refetches an entity from the database, so it's managed and definitely up to date (use when wanting to update entities from cache)
This commit is contained in:
parent
20e07c9140
commit
ded9c86054
|
@ -36,6 +36,7 @@ namespace App\Core;
|
||||||
|
|
||||||
use App\Util\Exception\DuplicateFoundException;
|
use App\Util\Exception\DuplicateFoundException;
|
||||||
use App\Util\Exception\NotFoundException;
|
use App\Util\Exception\NotFoundException;
|
||||||
|
use App\Util\Formatting;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Doctrine\Common\Collections\Criteria;
|
use Doctrine\Common\Collections\Criteria;
|
||||||
use Doctrine\Common\Collections\ExpressionBuilder;
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
||||||
|
@ -109,7 +110,7 @@ class DB
|
||||||
return array_search($class, self::$table_map);
|
return array_search($class, self::$table_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPKForClass(string $class)
|
public static function getPKForClass(string $class): array
|
||||||
{
|
{
|
||||||
return self::$class_pk[$class];
|
return self::$class_pk[$class];
|
||||||
}
|
}
|
||||||
|
@ -320,6 +321,17 @@ class DB
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function refetch(Entity $ent): ?Entity
|
||||||
|
{
|
||||||
|
$pks = self::getPKForClass($ent::class);
|
||||||
|
$criteria = [];
|
||||||
|
foreach ($pks as $pk) {
|
||||||
|
$method = Formatting::snakeCaseToCamelCase("get_{$pk}");
|
||||||
|
$criteria[$pk] = $ent->{$method}();
|
||||||
|
}
|
||||||
|
return self::findOneBy($ent::class, $criteria);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Intercept static function calls to allow referring to entities
|
* Intercept static function calls to allow referring to entities
|
||||||
* without writing the namespace (which is deduced from the call
|
* without writing the namespace (which is deduced from the call
|
||||||
|
|
Loading…
Reference in New Issue
Block a user