【发布时间】: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