0

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();
}
}
5
  • 1
    I assume $ad contains a list of ads posted by the current user. If these ads has a relation to category, and each category has a relation to its parent category, you should be able to do this in twig with just {% for post in posts %}Post {{ post.id }} has category {{ post.category.id }} ({{ post.category.parentCat.id }}{% endfor %} Note that this will require multiple queries to run in each iteration of the loop. You would be better off making a custom repository method to fetch a users ads while joining in the categories. Commented Nov 1, 2017 at 19:30
  • i tried to make a custom repository ( i will update the code in the question ) i cant figure out how to pass the argument - symfony trows something like catchable fatal error -- array problem Commented Nov 1, 2017 at 19:34
  • I suggest you make the repository method in the repository for the ads. If you there just join in the relations to the category/parent category you should get what you want from that repo only. Commented Nov 1, 2017 at 20:53
  • any new ideas ? Commented Nov 2, 2017 at 9:45
  • No I'm quite confident the way I've described it is the way to do it Commented Nov 2, 2017 at 15:34

1 Answer 1

1

Just a suggestion. Ideally, you should have had one category table with the self reference to it. This is how you can query for it. I am imagining that in the AdsList table you have reference to the CategoryAds Table.

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\CategoryAd")
 */
private $categoryAd;

Then, Your controller should look like this

public function listAction ()
{
    $agency = $this->get('security.token_storage')->getToken()->getUser();

    $ads = $this->getDoctrine()->getRepository('AppBundle:AdsList');
    $ad = $ads->findBy(array('postedBy' => $agency));
    return $this->render('agency/index.html.twig', [
        'user'  => $agency,
        'posts'=> $ad,
    ]);
}

Your twig file should reference to subcategory and main category like this

{% for post in posts %}
    {{post.categoryAd.name}} {# assuming you have category name #}
    {{post.categoryAd.parentCat.name}} {# assuming you have main category name #}
{% endfor%}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.