FindBy() 方法 "or" / "and"

FindBy() method "or" / "and"

如何在 findBy 方法中使用 or

示例:我有一个带有 id_user_oneid_user_two 的实体,我想通过 findby 方法搜索所有实体都是 id = 1 for id_user_oneid_user_two.

问题已发布 here 但没有人找到有效的回复...希望这是可能的 :)

P.S。 : 如果你也知道 and 是如何工作的,我会很高兴知道它:)

不,仅使用 findBy 是不可能做到这一点的。您要么必须自己编写存储库函数:

$repo->createQueryBuilder('e')
   ->andWhere('e.id_user_one = :user_id')
   ->orWhere('e.id_user_two = :user_id') // change to andWhere depending on your needs
   ->setParameter('user_id', 1)
   ->getQuery()->getResult();

或通过两次调用获取实体,然后合并结果:

$collection1 = $repo->findBy(['id_user_one' => 1]);
$collection2 = $repo->findBy(['id_user_two' => 1]);
$collection3 = new ArrayCollection(
    array_merge($collection1->toArray(), $collection2->toArray())
);

你的ps:$result = $repo->findBy(['id_user_one' => 1, 'id_user_two' => 1]);