How to perform logic if findBy’Field’ does match?

2019-09-19 17:09发布

问题:

I trying to do some logic if my inputed form email matches one found in the database.

How do I make the comparison if the findBy’Field” returns the whole collection instead of just the field I want to compare to? I'd think it should be done without using a foreach loop as that would kinda defeat the purpose of using my findBy method.

An Example:

$formEmail = $form->get('email')->getData();

existingEmail = $em->getRepository(‘UserBundle:User’)->findOneByEmail($formEmail);
// or existingEmail = $em->getRepository(‘UserBundle:User’)->findByEmail($formEmail);

if ($formEmail == $loggedEmail){
    //perform some logic here
}

回答1:

try this

$formEmail = $form->get('email')->getData();

$existingEmail = $em->getRepository('UserBundle:User')->findOneByEmail($formEmail);

if ($existingEmail){
    //found
}else{
    //not found
}