Symfony Doctrine findOneBy 不等于
Symfony Doctrine findOneBy with not equal to
我需要在合同 ID 不等于当前合同 ID 的记录中找到一个 phone 号码。
在具体的合约ID中很容易找到。 $value 是我的自定义验证器中的实体实例。
$existingPhone = $this->contractRepository->findOneBy(['phone' => $value->getPhone(), 'contractId' => $value->getContractId()]);
但是如何在当前合约 ID 以外的地方找到?
您需要在 contractRepository
中创建一个方法,并使用 Doctrine QB。
$qb = $this->createQueryBuilder('c');
$qb
->where('c.phone = :phone')
->andWhere(
$qb->expr()->neq('c.contractId', 'contractId')
)
->setParameters([
'phone' => $phone,
'contractId' => $contractId,
])
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
我需要在合同 ID 不等于当前合同 ID 的记录中找到一个 phone 号码。 在具体的合约ID中很容易找到。 $value 是我的自定义验证器中的实体实例。
$existingPhone = $this->contractRepository->findOneBy(['phone' => $value->getPhone(), 'contractId' => $value->getContractId()]);
但是如何在当前合约 ID 以外的地方找到?
您需要在 contractRepository
中创建一个方法,并使用 Doctrine QB。
$qb = $this->createQueryBuilder('c');
$qb
->where('c.phone = :phone')
->andWhere(
$qb->expr()->neq('c.contractId', 'contractId')
)
->setParameters([
'phone' => $phone,
'contractId' => $contractId,
])
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();