1

I am trying to compare an email address inputed from a form to what is already in the database and I figure the best way to do so is with using a findByEmail method.

I expected that to find the email address for a specific entry in the table but instead it returns the whole entry (first name, last name, and more…). How do I only find the email address of the entry in the table?

I know I can use a foreach to iterate through the entry but I think that kinda defeats the purpose of using a findByEmail function.

Here’s what I’ve tried so far:

$formEmail = $form->get('email')->getData();
$personEmail = $em->getRepository('UserBundle:User')->findByEmail($formEmail); //Should just find the email address of a person in the database. 
var_dump($personsEmail); //returns the whole row associated with the email address (first name, last name…..) 
var_dump(if($formEmail == $personEmail));die; //returns false when it should be true because it finds the whole row instead of the email address
grgre
  • 85
  • 7
  • It's not obvious what you are actually trying to achieve: if a row is found - it would have the email identical to the `$formEmail`, since you used it as a search criteria. – zerkms Dec 14 '16 at 20:49
  • In this line: `if($formEmail == $personEmail)` you are comparing a text value to a row value, and that won't work! – Alvin Bunk Dec 14 '16 at 20:54
  • @AlvinBunk sorry that was left over from me trying it in a `foreach` loop. `foreach ($personEmails as $email){$personEmail = email->getEmail(); var_dump($formEmail == $personEmail);}` – grgre Dec 14 '16 at 20:57
  • @zerkms yes that is what I'm trying to achieve but when comparing `$formEmail` to `$personEmail` and the emails are the same, it returns false! – grgre Dec 14 '16 at 20:58
  • @grgre if a row is found - they are guaranteed to be the same. What is the purpose of the comparison that would always return `true`? – zerkms Dec 14 '16 at 20:59
  • Possible duplicate of [How to perform logic if findBy’Field’ does match?](http://stackoverflow.com/questions/41166604/how-to-perform-logic-if-findby-field-does-match) – Joe Dec 15 '16 at 15:06

1 Answers1

0

If you fetched the entity successfully by email then the email must match.

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

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

    if ($person instanceof User) {

        // you found a user by email, so the email therefore must match ...        
    }

Note, use findOneBy not findBy, then you will fetch the object directly instead of in a collection.

Alternatively if you must fetch just the email address for comparison.

    $email = $em->createQueryBuilder()
        ->select('u.email')
        ->from('UserBundle:User', 'u')
        ->where('u.email = :email')
        ->setParameter('email', $formEmail)
        ->getQuery()
        ->getSingleScalarResult()
    ;

Note though that you could just use $person->getEmail() in my first example for the same result.

Richard
  • 4,079
  • 1
  • 14
  • 17
  • findOneBy and findBy returned the same thing, the whole collection – grgre Dec 14 '16 at 21:35
  • See documentation for findOneBy: http://www.doctrine-project.org/api/orm/2.5/source-class-Doctrine.ORM.EntityRepository.html#183-196 - returns an entity, not a collection. See also: http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/dql-doctrine-query-language.html#magic-finders - also see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-simple-conditions - if this is not working there may be something more fundamentally wrong with your setup. – Richard Dec 14 '16 at 23:25