i have the following route that displays a list of posts this list of posts also shows the category of the post each category has a parent category what i want is to show in a twig template the parent category that each posts corresponds to. my failed attempt is $rnc var which i only did it for a dump test
/**
* @Route("/agency", name="agency_admin")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listAction ()
{
$agency = $this->get('security.token_storage')->getToken()->getUser();
$ads = $this->getDoctrine()->getRepository('AppBundle:AdsList');
$ad = $ads->findBy(array('postedBy' => $agency));
$rnx = $this->getDoctrine()->getRepository('AppBundle:CategoryAd');
$rnc = $rnx->findBy(array('parentCat' => $ad));
return $this->render('agency/index.html.twig', [
'user' => $agency,
'posts' => $ad,
]);
}
CategoryAd Entity
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\MainCategory", inversedBy="subCat")
*/
private $parentCat;
MainCategory Entity
/**
* @var
* @ORM\OneToMany(targetEntity="AppBundle\Entity\CategoryAd", mappedBy="parentCat")
* @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
*/
protected $subCat;
i only posted the related fields from each Entity i hope i posted enough data. thank in advance
LE:
class CategoryRepository extends EntityRepository
{
/**
* @param CategoryAd $subCat
* @return CategoryAd[]
*/
public function findAllParentCat(CategoryAd $pc)
{
return $this->createQueryBuilder('ads_category_main')
->andWhere('ads_category_main.subcat = :sc')
->setParameter('sc', $pc)
->getQuery()
->execute();
}
}