2017-05-05 62 views
2

我想选择其他字段显示在我的表单复选框。如何在'选项'findall后选择表单的显示区域?

我的实体Filter有三个属性id,namesubtitle

我的代码显示我的name值,如何显示subtitle值?

我FormBuilder(控制器):

$formFilter = $this->createFormBuilder() 
     ->add('_',  ChoiceType::class,array(
       'choices' => $this->getDoctrine()->getManager()->getRepository('loicFilterBundle:Filter')->findAll(), 
       'multiple' => true, 
       'expanded' => true, 
       'choice_label' => function($value, $key, $index) { 
       return ($value); 
       }, 
       ))   
       ->add('Appliquer filtres', SubmitType::class) 

       ->getForm(); 

筛选:

namespace loic\FilterBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Filter 
* 
* * @ORM\Entity(repositoryClass="loic\FilterBundle\Entity\FilterRepository") 
* @ORM\Table(name="filter", uniqueConstraints={@ORM\UniqueConstraint(name="idfilter_UNIQUE", columns={"idfilter"})}, indexes={@ORM\Index(name="fk_filter_filter_category1_idx", columns={"filter_category_idfilter_category"})}) 
*/ 
class Filter 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="idfilter", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $idfilter; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=45, nullable=true) 
    */ 
    private $name; 

    /** 
    * @var \FilterCategory 
    * 
    * @ORM\ManyToOne(targetEntity="FilterCategory") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="filter_category_idfilter_category", referencedColumnName="idfilter_category") 
    * }) 
    */ 
    private $filterCategoryfilterCategory; 

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="loic\ContentBundle\Entity\Content", mappedBy="filterfilter") 
    */ 
    private $contentcontent; 

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="loic\UserBundle\Entity\User", mappedBy="filterfilter") 
    */ 
    private $user; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="subtitle", type="string", length=45, nullable=true) 
    */ 
    private $subtitle; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="description", type="string", length=45, nullable=true) 
    */ 
    private $description; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="status", type="string", length=45, nullable=false) 
    */ 
    private $status; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->contentcontent = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->user = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->status = 1; 
    } 

    /** 
    * 
    * @return the integer 
    */ 
    public function getIdfilter() { 
     return $this->idfilter; 
    } 

    /** 
    * 
    * @param 
    *   $idfilter 
    */ 
    public function setIdfilter($idfilter) { 
     $this->idfilter = $idfilter; 
     return $this; 
    } 

    /** 
    * 
    * @return the string 
    */ 
    public function getName() { 
     return $this->name; 
    } 

    /** 
    * 
    * @param 
    *   $name 
    */ 
    public function setName($name) { 
     $this->name = $name; 
     return $this; 
    } 

    /** 
    * 
    * @return the \FilterCategory 
    */ 
    public function getFilterCategoryfilterCategory() { 
     return $this->filterCategoryfilterCategory; 
    } 

    /** 
    * 
    * @param \FilterCategory $filterCategoryfilterCategory   
    */ 
    public function setFilterCategoryfilterCategory($filterCategoryfilterCategory) { 
     $this->filterCategoryfilterCategory = $filterCategoryfilterCategory; 
     return $this; 
    } 

    /** 
    * 
    * @return the \Doctrine\Common\Collections\Collection 
    */ 
    public function getContentcontent() { 
     return $this->contentcontent; 
    } 

    /** 
    * 
    * @param 
    *   $contentcontent 
    */ 
    public function setContentcontent($contentcontent) { 
     $this->contentcontent = $contentcontent; 
     return $this; 
    } 

    public function __toString(){ 

     return $this->name; 
    } 

    /** 
    * 
    * @return the \Doctrine\Common\Collections\Collection 
    */ 
    public function getUser() { 
     return $this->user; 
    } 

    /** 
    * 
    * @param 
    *   $user 
    */ 
    public function setUser($user) { 
     $this->user = $user; 
     return $this; 
    } 

    /** 
    * 
    * @return the string 
    */ 
    public function getSubtitle() { 
     return $this->subtitle; 
    } 

    /** 
    * 
    * @param 
    *   $subtitle 
    */ 
    public function setSubtitle($subtitle) { 
     $this->subtitle = $subtitle; 
     return $this; 
    } 

    /** 
    * 
    * @return the string 
    */ 
    public function getDescription() { 
     return $this->description; 
    } 

    /** 
    * 
    * @param 
    *   $description 
    */ 
    public function setDescription($description) { 
     $this->description = $description; 
     return $this; 
    } 

    /** 
    * 
    * @return the string 
    */ 
    public function getStatus() { 
     return $this->status; 
    } 

    /** 
    * 
    * @param 
    *   $status 
    */ 
    public function setStatus($status) { 
     $this->status = $status; 
     return $this; 
    } 



} 
+0

你能过去你的过滤器型号代码吗? – Alex

+0

为什么你应该通过“setUser”分配$ user? ... $ this-> user = new \ Doctrine \ Common \ Collections \ ArrayCollection(); ... 它是数据库实体还是字段名? – Alex

回答

3

你应该从外地开关类型来的EntityType(扩展ChoiceType)。

在那里你可以覆盖生成choice_label属性的方式。

use Symfony\Bridge\Doctrine\Form\Type\EntityType; 

$builder->add('category', EntityType::class, array(
    'class' => 'AppBundle:Category', 
    'choice_label' => function ($category) { 
     return $category->getDisplayName(); 
    } 
)); 

SORCE:http://symfony.com/doc/current/reference/forms/types/entity.html

0

谢谢它的作品。 ;)

此外,我们还可以只写字段名称,如:

'choice_label' => 'subtitle', . 

但是我不知道现场是如何与我以前的代码choosen。

+0

我可以告诉你。你实现了返回name属性的__toString()方法。当它呈现一个对象时,这将由树枝调用。 – Joe

+0

当然......不记得那种方法。谢谢! – L01C

相关问题