Doctrine findOneBy return values

14,606

Solution 1

You can test it using a var_dump($findByOneResult) and check what it return.

Dont forget to use exit() after so you will stop the code there.

I've just try this on one of my php project (Using Zendframework 2 and doctrine) and it return me only the first occurence of the database.

Hope it will help you.

Solution 2

findOneBy will return a single object based on the conditions given to it, if no data exists to return, then null value will be result.That you can check with empty() function. see this for more info http://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database, all the best

Share:
14,606
Martien de Jong
Author by

Martien de Jong

Updated on June 04, 2022

Comments

  • Martien de Jong
    Martien de Jong almost 2 years

    I am using Doctrine in a project where I have to find a single value. If such value does not exist or if there are multiple values another process has to be started. I figured that I should use the findOneBy function for this. Now I am wondering what this function does if the result is something else than a single value. Will it return null? Will it throw an error? I tried looking at the Doctrine documentation but could not find the place where it says what the return value or possible exceptions will be.

    It is important for me to know exactly what this function does because I have to use it in a critical process. An example of how to use it is not enough in my case. It would be really nice if there is some sort of API documentation.

    If someone could help me out it would be greatly appreciated. Thanks!

  • Martien de Jong
    Martien de Jong over 7 years
    Hello. Thanks for answering, and for testing. I will try it myself to see if I get what I need.
  • Martien de Jong
    Martien de Jong over 7 years
    Thank you for replying. I looked at the examples in the Doctrine manual, but I cannot find a place where it says what the return values and exceptions will be, just examples.
  • csalmeida
    csalmeida over 6 years
    This was really helpful to test how Doctrine works. Thanks KatharaDarko!