php – Doctrine ORM retornando array[0] no find

Question:

I have a question. I implemented Doctrine with Silex, created the entity, the repository and the service, but when I do a search with findByEmail() for example, to display the result I have to use $result[0] ->getName(). Would there be any way to remove this [0] from find? my method It looks like this:

public function fetchByEmail($email)
{
    $usuario = $this->em->getRepository($this->entity);

    return $usuario->findByEmail($email);

}

If you need any more information, just ask me… Grateful

Answer:

I managed to solve it… the problem is that the doctrine's findBy, returns an array with other arrays inside, so to return a single record I needed to use findOneBy then it solved my problem. Follow the code below:

public function fetchByEmail($email)
{
    $usuario = $this->em->getRepository($this->entity);

    return $usuario->findOneByEmail($email);

}
Scroll to Top