php - 不持久化数据的表单 Symfony 5
问题描述
您好,我无法使我的表单数据保持不变。它只在一侧起作用。
“目标”表单允许您创建目标并将它们链接到活动:没关系。
在我的“活动”表单中,我还有一个“目标”字段,它显示相关的目标,但我似乎无法修改它们。我的选择只存在于“目标”表单方面。
我只希望能够在活动页面上选择一些,仅此而已。
目标类型.php:
<?php
namespace App\Form;
use App\Entity\Target;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TargetType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('activity')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Target::class,
]);
}
}
活动类型.php:
<?php
namespace App\Form;
use App\Entity\Activity;
use App\Entity\Target;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ActivityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('targets', EntityType::class, [
'class' => Target::class,
'expanded' => false,
'multiple' => true,
])
->add('types')
->add('packs')
->add('partners')
->add('places')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Activity::class,
]);
}
}
目标实体:
<?php
namespace App\Entity;
use App\Repository\TargetRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TargetRepository::class)
*/
class Target
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity=Activity::class, inversedBy="targets", cascade={"persist"})
*/
private $activity;
public function __construct()
{
$this->activity = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Activity[]
*/
public function getActivity(): Collection
{
return $this->activity;
}
public function addActivity(Activity $activity): self
{
if (!$this->activity->contains($activity)) {
$this->activity[] = $activity;
}
return $this;
}
public function removeActivity(Activity $activity): self
{
if ($this->activity->contains($activity)) {
$this->activity->removeElement($activity);
}
return $this;
}
public function __toString()
{
return
$this->name;
}
}
活动实体:
<?php
namespace App\Entity;
use App\Repository\ActivityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=ActivityRepository::class)
*/
class Activity
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Assert\Valid()
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity=Target::class, mappedBy="activity",cascade={"persist"})
*/
private $targets;
/**
* @ORM\ManyToMany(targetEntity=Type::class, mappedBy="activity")
*/
private $types;
/**
* @ORM\ManyToMany(targetEntity=Pack::class, mappedBy="activity")
*/
private $packs;
/**
* @ORM\ManyToMany(targetEntity=Partner::class, mappedBy="activity")
*/
private $partners;
/**
* @ORM\ManyToMany(targetEntity=Place::class, mappedBy="activity")
*/
private $places;
/**
* @ORM\OneToMany(targetEntity=Gallery::class, mappedBy="activity")
*/
private $image;
public function __construct()
{
$this->targets = new ArrayCollection();
$this->types = new ArrayCollection();
$this->packs = new ArrayCollection();
$this->partners = new ArrayCollection();
$this->places = new ArrayCollection();
$this->image = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Target[]
*/
public function getTargets(): Collection
{
return $this->targets;
}
public function addTarget(Target $target): self
{
if (!$this->targets->contains($target)) {
$this->targets[] = $target;
$target->addActivity($this);
}
return $this;
}
public function removeTarget(Target $target): self
{
if ($this->targets->contains($target)) {
$this->targets->removeElement($target);
$target->removeActivity($this);
}
return $this;
}
/**
* @return Collection|Type[]
*/
public function getTypes(): Collection
{
return $this->types;
}
public function addType(Type $type): self
{
if (!$this->types->contains($type)) {
$this->types[] = $type;
$type->addActivity($this);
}
return $this;
}
public function removeType(Type $type): self
{
if ($this->types->contains($type)) {
$this->types->removeElement($type);
$type->removeActivity($this);
}
return $this;
}
/**
* @return Collection|Pack[]
*/
public function getPacks(): Collection
{
return $this->packs;
}
public function addPack(Pack $pack): self
{
if (!$this->packs->contains($pack)) {
$this->packs[] = $pack;
$pack->addActivity($this);
}
return $this;
}
public function removePack(Pack $pack): self
{
if ($this->packs->contains($pack)) {
$this->packs->removeElement($pack);
$pack->removeActivity($this);
}
return $this;
}
/**
* @return Collection|Partner[]
*/
public function getPartners(): Collection
{
return $this->partners;
}
public function addPartner(Partner $partner): self
{
if (!$this->partners->contains($partner)) {
$this->partners[] = $partner;
$partner->addActivity($this);
}
return $this;
}
public function removePartner(Partner $partner): self
{
if ($this->partners->contains($partner)) {
$this->partners->removeElement($partner);
$partner->removeActivity($this);
}
return $this;
}
/**
* @return Collection|Place[]
*/
public function getPlaces(): Collection
{
return $this->places;
}
public function addPlace(Place $place): self
{
if (!$this->places->contains($place)) {
$this->places[] = $place;
$place->addActivity($this);
}
return $this;
}
public function removePlace(Place $place): self
{
if ($this->places->contains($place)) {
$this->places->removeElement($place);
$place->removeActivity($this);
}
return $this;
}
/**
* @return Collection|Gallery[]
*/
public function getImage(): Collection
{
return $this->image;
}
public function addImage(Gallery $image): self
{
if (!$this->image->contains($image)) {
$this->image[] = $image;
$image->setActivity($this);
}
return $this;
}
public function removeImage(Gallery $image): self
{
if ($this->image->contains($image)) {
$this->image->removeElement($image);
// set the owning side to null (unless already changed)
if ($image->getActivity() === $this) {
$image->setActivity(null);
}
}
return $this;
}
public function __toString()
{
return
$this->name;
}
}
解决方案
您需要通过将属性设置为来强制调用Target
您的 setter :ActivityType
by_reference
false
class ActivityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('targets', EntityType::class, [
'class' => Target::class,
'expanded' => false,
'multiple' => true,
'by_reference' => false // <- done in this line here
])
// ...
此处文档中的更多信息: https ://symfony.com/doc/current/reference/forms/types/collection.html#by-reference
推荐阅读
- java - 如何将JTable中的数据保存到数据库?
- python - pip freeze 不显示包版本
- python - 如何在 Kivy 中删除动态添加的 BoxLayout
- android-fragments - 如何在 TabLayout 中找到由 RecyclerView 打开的对话框的上下文?
- avro - Avro 文件从 Snowflake 表加载和读取
- javascript - 在Vue js中将复选框选择的值分组到数组中
- dart - 如何检查飞镖中的两个地图是否相等
- javascript - 将对象插入数组遇到访问问题(Nodejs)
- c++ - 在 Visual Studio 中使用括号但传入 g++/clang++ 时编译错误
- c++ - 错误:#include 在 Dev C++ 中嵌套太深