Doctrine 实体管理器通过空数组集合查找
Doctrine entity manager find by empty array collection
我有一个名为 Cycle 的实体,它与 CycleActeur 有 OneToMany 关联(见下面的代码)。
我希望能够从我的控制器中使用一个简单的学说 findBy* 方法,获取数据库中所有没有关联 CycleActeur 对象的 Cycle 对象。
也就是这样说:
$manager = $this->getContainer()->get('doctrine.orm.entity_manager');
$cycleManager = $manager->getRepository('ESI67Zen2Bundle:Cycle');
$cyclesWithNoCycleActeur = $cycleManager->findBy('acteurs', null);
有没有一种方法可以做到这一点而不必在 CycleRepository 中编写特定的方法?
从循环中提取class代码
class Cycle {
/**
* @ORM\OneToMany(
* targetEntity="CycleActeur",
* mappedBy="cycle",
* orphanRemoval=true)
*/
private $acteurs;
}
从循环中提取class代码
class CycleActeur {
/**
* @var Cycle Le cycle concerné
*
* @ORM\ManyToOne(targetEntity="Cycle", inversedBy="acteurs")
* @ORM\JoinColumn(name="cycle_id", referencedColumnName="id")
*
*/
private $cycle;
}
您的 Cycle 实体是关系的反面,它在数据库中的 table 没有 'acteurs' 列,因此您不能使用 findBy(['acteurs'=>null])
或 findByActeurs(null)
。但无论如何你可以做点什么:
$manager = $this->getContainer()->get('doctrine.orm.entity_manager');
$cycleManager = $manager->getRepository('ESI67Zen2Bundle:Cycle');
$allCycles = $cycleManager->findAll();
$cyclesWithNoCycleActeur = [];
foreach($allCycles as $cycle)
{
if($cycle->getActeurs()->isEmpty())
{
$cyclesWithNoCycleActeur[] = $cycle;
}
}
有一个 DQL 函数 SIZE()
,根据 Doctrine 文档:
SIZE(collection) - Return the number of elements in the specified collection
因此您可以将其用作条件,例如:
SIZE(acteurs) = 0
我不确定它是否适用于 findBy
方法,但我建议在 ESI67Zen2Bundle:Cycle
的存储库中创建一个自定义方法,以明确代码是什么正在做。它适用于 DQL 查询和查询生成器。
$cyclesWithNoCycleActeur = $cycleManager->findBy(array('SIZE(acteurs)' => 0));
我的 2 美分
在这种情况下(在我看来)最好的方法是使用 DQL 的条件 IS EMPTY
:
$manager
->createQueryBuilder()
->from(Cycle::class, 'cycle')
->select('cycle')
->andWhere('cycle.acteurs IS EMPTY')
->getQuery()
->getResult()
;
您可以在 EntityRepository 或您有权访问 EntityManager 的任何地方使用此代码。
我有一个名为 Cycle 的实体,它与 CycleActeur 有 OneToMany 关联(见下面的代码)。
我希望能够从我的控制器中使用一个简单的学说 findBy* 方法,获取数据库中所有没有关联 CycleActeur 对象的 Cycle 对象。
也就是这样说:
$manager = $this->getContainer()->get('doctrine.orm.entity_manager');
$cycleManager = $manager->getRepository('ESI67Zen2Bundle:Cycle');
$cyclesWithNoCycleActeur = $cycleManager->findBy('acteurs', null);
有没有一种方法可以做到这一点而不必在 CycleRepository 中编写特定的方法?
从循环中提取class代码
class Cycle {
/**
* @ORM\OneToMany(
* targetEntity="CycleActeur",
* mappedBy="cycle",
* orphanRemoval=true)
*/
private $acteurs;
}
从循环中提取class代码
class CycleActeur {
/**
* @var Cycle Le cycle concerné
*
* @ORM\ManyToOne(targetEntity="Cycle", inversedBy="acteurs")
* @ORM\JoinColumn(name="cycle_id", referencedColumnName="id")
*
*/
private $cycle;
}
您的 Cycle 实体是关系的反面,它在数据库中的 table 没有 'acteurs' 列,因此您不能使用 findBy(['acteurs'=>null])
或 findByActeurs(null)
。但无论如何你可以做点什么:
$manager = $this->getContainer()->get('doctrine.orm.entity_manager');
$cycleManager = $manager->getRepository('ESI67Zen2Bundle:Cycle');
$allCycles = $cycleManager->findAll();
$cyclesWithNoCycleActeur = [];
foreach($allCycles as $cycle)
{
if($cycle->getActeurs()->isEmpty())
{
$cyclesWithNoCycleActeur[] = $cycle;
}
}
有一个 DQL 函数 SIZE()
,根据 Doctrine 文档:
SIZE(collection) - Return the number of elements in the specified collection
因此您可以将其用作条件,例如:
SIZE(acteurs) = 0
我不确定它是否适用于 findBy
方法,但我建议在 ESI67Zen2Bundle:Cycle
的存储库中创建一个自定义方法,以明确代码是什么正在做。它适用于 DQL 查询和查询生成器。
$cyclesWithNoCycleActeur = $cycleManager->findBy(array('SIZE(acteurs)' => 0));
我的 2 美分
在这种情况下(在我看来)最好的方法是使用 DQL 的条件 IS EMPTY
:
$manager
->createQueryBuilder()
->from(Cycle::class, 'cycle')
->select('cycle')
->andWhere('cycle.acteurs IS EMPTY')
->getQuery()
->getResult()
;
您可以在 EntityRepository 或您有权访问 EntityManager 的任何地方使用此代码。