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我只为转储测试做了</ p> \ n

  / ** 
 * @Route(“/ agency”,name =“agency_admin”)
 * @return \ Symfony \ Component \ HttpFoundation \ Response 
 * / 
 npublic function listAction()  
 {
 $ agency = $ this-&gt; get('security.token_storage') - &gt; getToken() - &gt; getUser(); 
 
 $ ads = $ this-&gt; getDoctrine() -  &gt; getRepository('AppBundle:AdsList'); 
 $ ad = $ ads-&gt; findBy(array('postedBy'=&gt; $ agency)); 
 $ rnx = $ this-&gt; getDoctrine() -  &gt; getRepository('AppBundle:CategoryAd'); 
 $ rnc = $ rnx-&gt; findBy(array('parentCat'=&gt; $ ad)); 
返回$ this-&gt; render('agency / index  .html.twig',[
'用户'=&gt; $ agency,
'发布'=&gt; $ ad,\  n]); 
} 
 </ code> </ pre> 
 
 

CategoryAd </ strong>实体</ p>

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

MainCategory </ strong>实体</ p>

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

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

LE:</ p>

  class CategoryRepository扩展了EntityRepository 
 {
 / ** 
 * @param CategoryAd $ subCat  
 * @return CategoryAd [] 
 * / 
共和函数findAllParentCat(CategoryAd $ pc)
 {
返回$ this-&gt; createQueryBuilder('ads_category_main')
  - &gt; andWhere('ads_category_main.subcat =  :sc')
  - &gt; setParameter('sc',$ pc)
  - &gt; getQuery()
  - &gt; execute(); 
} 
} 
 </ code> </ pre  > 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题