【问题标题】:How to choose displayed field of a form after 'choices' findall?如何在“选择”findall之后选择表单的显示字段?
【发布时间】:2017-05-05 11:42:05
【问题描述】:

我想选择另一个字段显示在我的表单复选框中。

我的实体Filter 具有三个属性idnamesubtitle

我的代码显示我的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;
    }



}

【问题讨论】:

  • 你能把你的过滤器型号代码过去吗?
  • 为什么在 $user 应该由“setUser”分配的时候分配它? ... $this->user = new \Doctrine\Common\Collections\ArrayCollection(); ... 是数据库实体还是字段名?

标签: forms symfony doctrine


【解决方案1】:

您应该将字段类型切换为 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();
    }
));

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

【讨论】:

    【解决方案2】:

    谢谢它的工作。 ;)

    另外,我们可以把字段名写成这样:

    'choice_label' => 'subtitle', .
    

    但是我想知道我以前的代码是如何选择该字段的。

    【讨论】:

    • 我可以告诉你。您实现了返回 name 属性的 __toString() 方法。 twig 在渲染对象时会调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多