Doctrine ORM returning array[0] in find

Asked

Viewed 132 times

3

I have a question. I implemented Doctrine with Silex, created the entity, the Repository and the service, but when it comes to doing a search with findByEmail() for example, to display the result I have to use $result[0]->getNome(). Would there be any way to remove that [0] from find? my method is 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

  • public Function fetchByEmail($email) { $usuario = $this->em->getRepository($this->Entity); Return $usuario->findByEmail($email)[0]; }, would not solve your problem?

  • Thanks for the help Denis.. but is that there is no other way to do this without having to pass the [0] somewhere?

  • I’m researching, it’s actually know nothing about Doctrine, I replied in comment because I knew that the query response is an array, hence the need of [0]

  • 1

    Yes, tbm to all day searching.. I can’t find any reference.. but let’s continue, there must be some form.. and tbm am not very good with Doctrine.. to starting now...

  • could post findByEmail code?

  • This is a magical method of Doctrine.. I didn’t make it myself....

  • I managed to solve. the problem is that Doctrine findBy, returns an array with other arrays inside, then to return a single record I needed to use findOneBy there solved my problem.. Thanks Denis for the help..

  • You are welcome to create an answer to your own question and mark it as accepted to help other people who have the same problem

Show 4 more comments

1 answer

2


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

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

    return $usuario->findOneByEmail($email);

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.