user1052836
user1052836

Reputation: 329

How to find a record from a repository

How can I find a record from a repository in the controller:

$em = $this->getDoctrine()->getRepository('ShopMyShopBundle:Product')->find($value);

Can I do something like this in template ?

{{ em.name }}

Upvotes: 0

Views: 4556

Answers (2)

kuboslav
kuboslav

Reputation: 1460

It depends on what is in $value. Method find() will finb by id. If you want to search by for example slug, you have to call method findBySlug() or equivalent findOneBySlug().

Upvotes: 1

Anton Babenko
Anton Babenko

Reputation: 6636

In a controller:

$em = $this->get('doctrine')->getEntityManager();
$product = $em->getRepository('ShopMyShopBundle:Product')->find($value);

You can not do this in template. Feel free to read documentation.

Upvotes: 0

Related Questions