doumeng3080 2017-11-01 19:10
浏览 41
已采纳

symfony - 从数组传递字段实体

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();
}
}
  • 写回答

1条回答 默认 最新

  • douchi0028 2017-11-02 19:42
    关注

    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%}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单