不保留数据的表单 Symfony 5
Form that does not persist data Symfony 5
您好,
我无法使我的表单数据持久化。它只对一侧有效。
'Target' 表单允许您创建目标并将它们 link 用于活动:没关系。
在我的 'Activities' 表单中,我还有一个 'Target' 字段显示相关的目标,但我似乎无法修改它们。我的选择只保留在 'Target' 表单方面。
我只想在 activity 页面上 select 一些,仅此而已。
TargetType.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,
]);
}
}
ActivityType.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;
}
}
Activity实体:
<?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;
}
}
您需要通过将 by_reference
属性 设置为 false
来强制调用 ActivityType
中 Target
的 setter ] :
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
您好, 我无法使我的表单数据持久化。它只对一侧有效。
'Target' 表单允许您创建目标并将它们 link 用于活动:没关系。
在我的 'Activities' 表单中,我还有一个 'Target' 字段显示相关的目标,但我似乎无法修改它们。我的选择只保留在 'Target' 表单方面。
我只想在 activity 页面上 select 一些,仅此而已。
TargetType.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,
]);
}
}
ActivityType.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;
}
}
Activity实体:
<?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;
}
}
您需要通过将 by_reference
属性 设置为 false
来强制调用 ActivityType
中 Target
的 setter ] :
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