更新时间:2023-02-23 20:19:19
return (boolean)$this->createQueryBuilder('u')
->andWhere('u.email = :email AND u.id != :id')
->setParameter('email', $email)
->setParameter('id', $userId)
->getQuery()
->getOneOrNullResult();
请注意,如果有可能(例如,电子邮件列中没有唯一索引),查询返回多个结果,然后还需要用try / catch块包装该调用,因为如果找到多个结果,它可能会引发异常。
Please note that if it is possible (for example there is no unique index on email column) that the query returns multiple results, then you also need to wrap the call with try/catch block, because it might throw exception if more than one result are found.