I'm creating a registration form and want to check to see if an email is not already associated with an account. All the ways I can see will create the entire User entity, but I just need to know if it exists.
public function isUnusedEmail($email) {
$em = static::$pimple['em'];
$dql = 'SELECT 1 FROM App\Model\User user WHERE user.email = :email';
$query = $em->createQuery($dql);
$query->setParameter('email', $email);
$res = $query->getResult();
return empty($res);
}
Consider the following method:
/**
* @param string $token
* @return bool
*/
public function isTokenUnique($token)
{
$manager = $this->getEntityManager();
/** @var Doctrine\ORM\Query $query */
$query = $manager->
createQuery('SELECT 1 FROM AppBundle:Member m WHERE m.token = :token')
->setParameter('token', $token)
->setMaxResults(1)
;
return (count($query->getResult()) == 0);
}
Notice a call to setMaxResults()
. It can be important in some cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With