如何从两个实体存储库中提取相似的 Doctrine 函数?
How to dry-up similar Doctrine functions from two Entity Repositories?
我正在使用 Symfony 4。
namespace App\Repository;
use ...
class CountryRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Country::class);
}
...
public function deleteMultipleByIds(array $ids): void
{
$builder = $this->createQueryBuilder('l');
$builder->delete()
->where('l.id IN(:ids)')
->setParameter(
':ids',
$ids,
Connection::PARAM_INT_ARRAY
);
$query = $builder->getQuery();
$query->execute();
}
CountryI18nRepository
class.
中存在相同的方法
我希望只有一个这样的函数,它将只使用正确的实体 (Country v CountryI18n)。
如何以及在哪里创建新的 class? class 应该属于 ServiceEntitiyRepository
class 还是属于哪个?
如果您的问题是关于重复的,您可以制作一个 GenericRepo
(不一定是学说存储库;请选择一个更好的名称),您可以在需要的地方注入和使用。
类似
class GenericRepo
{
public function deleteMultipleByIds(QueryBuilder $qb, string $rootAlias, array $ids): void
{
$qb->delete()
->where(sprintf('%s.id IN(:ids)', $rootAlias))
->setParameter(':ids', $ids, Connection::PARAM_INT_ARRAY);
$qb->getQuery()->execute();
}
}
而在你的,例如 CountryI18nRepository
class CountryI18nRepository
{
private $genericRepo;
public function __construct(GenericRepo $genericRepo)
{
$this->genericRepo = $genericRepo;
}
public function deleteMultipleByIds(array $ids): void
{
$builder = $this->createQueryBuilder('l');
$this-> genericRepo->deleteMultipleByIds($builder, 'l', $ids);
}
}
您也可以从 GenericRepo
进行扩展,但是,由于 PHP 仅支持单继承,因此(至少在我看来)最好使用如上所示的组合。
免责声明
我没有测试此代码,因此可能需要进行一些调整。顺便说一句,概念是有效的。
使用 deleteMultipleByIds 创建一个抽象存储库,例如:
abstract class BaseCountryRepository extends ServiceEntityRepository
并在其他 CountryRepositories 中扩展它而不是 ServiceEntityRepository
class CountryRepository extends BaseCountryRepository
class CountryI18nRepository extends BaseCountryRepository
您可以从这些 类
中删除 deleteMultipleByIds 定义
我正在使用 Symfony 4。
namespace App\Repository;
use ...
class CountryRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Country::class);
}
...
public function deleteMultipleByIds(array $ids): void
{
$builder = $this->createQueryBuilder('l');
$builder->delete()
->where('l.id IN(:ids)')
->setParameter(
':ids',
$ids,
Connection::PARAM_INT_ARRAY
);
$query = $builder->getQuery();
$query->execute();
}
CountryI18nRepository
class.
我希望只有一个这样的函数,它将只使用正确的实体 (Country v CountryI18n)。
如何以及在哪里创建新的 class? class 应该属于 ServiceEntitiyRepository
class 还是属于哪个?
如果您的问题是关于重复的,您可以制作一个 GenericRepo
(不一定是学说存储库;请选择一个更好的名称),您可以在需要的地方注入和使用。
类似
class GenericRepo
{
public function deleteMultipleByIds(QueryBuilder $qb, string $rootAlias, array $ids): void
{
$qb->delete()
->where(sprintf('%s.id IN(:ids)', $rootAlias))
->setParameter(':ids', $ids, Connection::PARAM_INT_ARRAY);
$qb->getQuery()->execute();
}
}
而在你的,例如 CountryI18nRepository
class CountryI18nRepository
{
private $genericRepo;
public function __construct(GenericRepo $genericRepo)
{
$this->genericRepo = $genericRepo;
}
public function deleteMultipleByIds(array $ids): void
{
$builder = $this->createQueryBuilder('l');
$this-> genericRepo->deleteMultipleByIds($builder, 'l', $ids);
}
}
您也可以从 GenericRepo
进行扩展,但是,由于 PHP 仅支持单继承,因此(至少在我看来)最好使用如上所示的组合。
免责声明
我没有测试此代码,因此可能需要进行一些调整。顺便说一句,概念是有效的。
使用 deleteMultipleByIds 创建一个抽象存储库,例如:
abstract class BaseCountryRepository extends ServiceEntityRepository
并在其他 CountryRepositories 中扩展它而不是 ServiceEntityRepository
class CountryRepository extends BaseCountryRepository
class CountryI18nRepository extends BaseCountryRepository
您可以从这些 类
中删除 deleteMultipleByIds 定义