I have two entities Category
and Item
. I want to access all items under a particular category.
Presently, I'm doing this as follows:
- Get entity corresponding to given category
- Get all Items by passing
category
selected in previous step as a parameter tofindBy
method.
Here is my code:
public function indexAction($category)
{
$em = $this->getDoctrine()->getManager();
$category = $em -> getRepository('AppBundle:Category')
-> findOneBy(array(
"name" => $category
));
$entities = $em->getRepository('AppBundle:Item')
->findBy(array(
'category' => $category
));
return array(
'entities' => $entities,
'title' => $category
);
}
Am I doing right? In this case I need two separate queries. Is there any efficient method?