【问题标题】:Render a filtered checkbox collection in a form在表单中呈现过滤的复选框集合
【发布时间】:2019-04-26 09:13:47
【问题描述】:

我想将过滤后的集合呈现为复选框列表。 但我很难让收藏品展示出来。我得到“可捕获的致命错误:类 Doctrine\ORM\PersistentCollection 的对象无法转换为字符串”。

下面是我的表单类型:

class PropertyfilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('view', EntityType::class, [
                'class' => Propsearch::class,
                'choice_label' => 'propsearchviews',
               'expanded' => true,
                'multiple' => true

            ]);
}

这是我的多对多实体

<?php

namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;


/**
 */
class Propsearch
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;



   /**
     * @var Propsearchview[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Propview", cascade={"persist"})
     * @ORM\JoinTable(name="propsearch_propview")
     * @ORM\OrderBy({"title": "ASC"})
     * @Assert\Count(max="4", maxMessage="Can only select 4 views")
     */
    private $propsearchviews;



   /**
     * @var Propsearchfacility[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Propfacility", cascade={"persist"})
     * @ORM\JoinTable(name="propsearch_propfacility")
     * @ORM\OrderBy({"title": "ASC"})
     * @Assert\Count(max="4", maxMessage="Can only select 4 facilities")
     */
    private $propsearchfacilities;



    public function getId(): ?int 
    {
        return $this->id;
    }




    public function __construct()
    {

        $this->propsearchviews = new ArrayCollection();
        $this->propsearchfacilities = new ArrayCollection();
    }




   /**
     * @return Collection|Propsearchview[]
     */
    public function getPropsearchviews(): Collection
    {
        return $this->propsearchviews;
    }



    public function addPropsearchview(Propsearchview $propsearchview): self
    {
        if (!$this->propsearchviews->contains($propsearchview)) {
            $this->propsearchviews[] = $propsearchview;
        }
        return $this;
    }



    public function removePropsearchview(Propsearchview $propsearchview): self
    {
        if ($this->propsearchviews->contains($propsearchview)) {

            $this->propsearchviews->removeElement($propsearchview);
        }
        return $this;
    }






    /**
     * @return Collection|Propsearchfacility[]
     */
    public function getPropsearchfacilities(): Collection
    {
        return $this->propsearchfacilities;
    }


    public function addPropsearchfacility(Propsearchfacility $propsearchfacility): self
    {
        if (!$this->propsearchfacilities->contains($propsearchfacility)) {

            $this->propsearchfacilities[] = $propsearchacility;
        }
        return $this;
    }



    public function removePropsearchfacility(Propsearchfacility $propsearchfacility): self
    {
        if ($this->propsearchfacilities->contains($propsearchfacility)) {

            $this->propsearchfacilities->removeElement($propsearchfacility);
        }
        return $this;
    }





}

这是我原来的视图实体。

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="propview")
 *
 * Defines the properties of the Tag entity to represent the post tags.
 *
 * See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class
 *
 * @author Yonel Ceruto <yonelceruto@gmail.com>
 */
class Propview 
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;


    /**
     * @var string
     *
     * @ORM\Column(type="string", length=191)
     */
    private $title;




    public function getId(): ?int
    {
        return $this->id;
    }


    public function getTitle(): ?string
    {
        return $this->title;
    }


    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }



    public function __toString(): string
    {

        return $this->title;
    }



}

所以我想将视图集合显示为一个复选框列表,该列表已添加到表单中的 propsearch 表中。 提前致谢!


编辑 2 好的,所以我有 propsearchviews,它有一个来自 propviewtype 的集合。包括来自 propsearch 的数据类。

我将 propertyfiltertype 更改为以下内容:

    <?php

    namespace App\Form;

    use App\Entity\Propsearch;

    class PropertyfilterType extends AbstractType
    {
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder
        ->add('propsearchviews', CollectionType::class, [
            'entry_type' => PropviewType::class,
            'by_reference' => false,
        ]);




}

propviewtype 本身

namespace App\Form\Type;

use App\Entity\Propview;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

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

class PropviewType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{


    $builder

        ->add('propview', EntityType::class, [
        'class' => Propview::class,
        'choice_label' => 'title',

        ]);



/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => Propview::class,
    ));
}

}

还有我的 html.twig 文件

<div class="col-12 col-md-4 mb-2">


{% for field in propertybuyform.propsearchviews %}
    <div class="col-xs-4">
        {{ form_widget(field) }}
        {{ form_label(field) }}
    </div>
{% endfor %}



</div>

【问题讨论】:

  • 我认为,关于表单,不同实体如何协同工作以及您的目标是什么还不太清楚。使用 CollectionType 可能是正确的解决方案,但我也可能是错误的解决方案。 CollectionType 旨在处理相同类型的多个子表单(即:沿“父”实体编辑子实体)。 EntityType(带有多个和扩展)旨在对(通常存在的)实体进行多选。自从你从 EntityType 转移到 CollectionType... 我想我会问...
  • @Jakumi,我搬家是因为有建议/试用。我的目标是以另一种形式(搜索形式)呈现实体的某个数组集合。我还想首先从实体类型开始,然后通过查询构建器获取集合。
  • 我做了一个查询构建器,但是这个不工作。 ->add('propsearchviews2', EntityType::class, [ 'class' => Propsearch::class, 'query_builder' => function (EntityRepository $er) { return $er->createQueryBuilder('a') ->innerJoin ('a.propsearchviews','b') // ->where('a.id = :propview_id') ->andWhere('b.id = :propsearch_id') ->setParameter('propsearch_id', 6); },

标签: symfony collections many-to-many entity formbuilder


【解决方案1】:

您应该使用嵌入式表单功能来实现这一点。请参阅https://symfony.com/doc/current/form/form_collections.html 了解如何实施。 简要描述您的情况 - 您应该创建 PropsearchType,它将 propsearchviews 属性呈现为 CollectionType,其中“entry_type”将是您应该创建的另一种自定义表单类型 - PropviewType,它应该将您的 Propviews 呈现为复选框。

【讨论】:

  • 感谢您的反馈。我会调查一下。我很难理解嵌入式表单功能,因为我仍在学习所有 symfonys 功能。如果你有更多的开始,我非常感激!
  • 我查看了嵌入式表单功能和您的建议。我得到了嵌入式集合。但是,您将如何呈现有限的 propviews?所以只有已经添加到 propsearchviews 集合中的 propviews...
  • 如果你把一切都做对了,你应该只收集那些在收藏中的东西。请将问题中的代码更新为实际状态,以便我们查看。
  • 我更新了我的编辑代码。我很困惑如何设置它:/我试图按照你的链接中的代码嵌入代码。
  • 我更新到我最新的代码,尝试不同的方法,但我迷路了:/
猜你喜欢
  • 2021-11-27
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多