stivlo stivlo - 16 days ago 5
PHP Question

Symfony/Doctrine

I am currently learning Symfony and Doctrine by reading the docs.

I don't understand the difference between find and findOneById. I tried to use them both in this simple example and it looks they do the same thing to me.

$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:ProductEntity')
->findOneById($id);


Are they really the same thing or there is some difference? And where I can find the detailed documentation for all these methods?

rid rid
Answer

In your case, they happen to do the same thing. Looking at this example, you'll notice that find() looks for the field named after the primary key. findOneBy<Field>() will explicitly use the field in the name of the method, even if it's not the primary key, and will return the first record. So, in the end, if the primary key is indeed named id, then both will do the same thing.

// query by the primary key (usually "id")
$product = $repository->find($id);

// dynamic method names to find based on a column value
$product = $repository->findOneById($id);
$product = $repository->findOneByName('foo');
Comments