Doctorine check if record exists in database [duplicate]

吃可爱长大的小学妹 提交于 2020-05-17 03:43:02

问题


I would like to check if a record exist in the database. The only thing it needs to return is true or false. I'm now using the following query to get the record. Is the an other command as getResult() to check if the record exists?

return $this->createQueryBuilder('u')
            ->andWhere('u.email = :email AND u.id != :id')
            ->setParameter('email', $email)
            ->setParameter('id', $userId)
            ->getQuery()
            ->getResult();

回答1:


return (boolean)$this->createQueryBuilder('u')
            ->andWhere('u.email = :email AND u.id != :id')
            ->setParameter('email', $email)
            ->setParameter('id', $userId)
            ->getQuery()
            ->getOneOrNullResult();

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.



来源:https://stackoverflow.com/questions/36801089/doctorine-check-if-record-exists-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!