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();
}
}

图片转代码服务由CSDN问答提供 功能建议

我有以下路线显示帖子列表 这个帖子列表还显示了帖子的类别\ neach类别有一个父类别 我想要的是在树枝模板中显示每个帖子对应的父类别。 尝试失败是$ rnc var我只为转储测试做了 \ n

  / ** 
 * @Route(“/ agency”,name =“agency_admin”)
 * @return \ Symfony \ Component \ HttpFoundation \ Response 
 * / 
 npublic 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)); 
返回$ this-> render('agency / index  .html.twig',[
'用户'=> $ agency,
'发布'=> $ ad,\  n]); 
} 
   
 
 

CategoryAd 实体

  / **  
 * @ORM \ ManyToOne(targetEntity =“AppBundle \ Entity \ MainCategory”,inversedBy =“subCat”)
 * / 
private $ parentCat; 
   
 
 

MainCategory 实体

  / ** 
 * @var 
 * @ORM \ OneToMany(targetEntity =“AppBundle \ Entity \ CategoryAd”,mappedBy  =“parentCat”)
 * @ORM \ JoinColumn(name =“cat_id”,referencedColumnName =“id”)
 * / 
protected $ subCat; 
   
 
 

我只发布了每个实体的相关字段 i希望我发布了足够的数据。 谢谢提前

LE:

  class CategoryRepository扩展了EntityRepository 
 {
 / ** 
 * @param CategoryAd $ subCat  
 * @return CategoryAd [] 
 * / 
共和函数findAllParentCat(CategoryAd $ pc)
 {
返回$ 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%}
    
    点赞 打赏 评论

相关推荐 更多相似问题