Symfony4 使用预设数据并更改值
我正在尝试恢复数据并根据选择器修改它们。这就是我的应用程序的工作原理:我有与文章相关的供应商,几个供应商可以拥有相同的产品,除了参考不同的供应商,我正在寻找能够根据供应商修改此参考的可能性 实体供应商:Symfony4 使用预设数据并更改值,symfony4,Symfony4,我正在尝试恢复数据并根据选择器修改它们。这就是我的应用程序的工作原理:我有与文章相关的供应商,几个供应商可以拥有相同的产品,除了参考不同的供应商,我正在寻找能够根据供应商修改此参考的可能性 实体供应商: <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Symfony\Component\
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\FournisseursRepository")
* @Vich\Uploadable
*/
class Fournisseur
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $category;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $civility;
/**
* @ORM\Column(type="text")
*
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $company;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $named;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $surname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $building;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $street;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $other;
/**
* @ORM\Column(type="integer", length=5)
*
* @var integer
*/
private $zipcode;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $city;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $country;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $url;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $login;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $passwordSite;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="fournisseur_image", fileNameProperty="imageName", size="imageSize")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
private $imageSize;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $type;
/**
* @ORM\Column(type="string", length=255)
*/
private $phone1;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $phone2;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fax;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="text")
*/
private $note;
/**
* @ORM\OneToMany(targetEntity="Reference", mappedBy="fournisseur")
*/
private $references;
public function __construct() {
$this->references = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return string
*/
public function getCategory(): ?string
{
return $this->category;
}
/**
* @param string $category
*/
public function setCategory(string $category): void
{
$this->category = $category;
}
/**
* @return string
*/
public function getCivility(): ?string
{
return $this->civility;
}
/**
* @param string $civility
*/
public function setCivility(string $civility): void
{
$this->civility = $civility;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return string
*/
public function getCompany(): ?string
{
return $this->company;
}
/**
* @param string $company
*/
public function setCompany(string $company): void
{
$this->company = $company;
}
/**
* @return string
*/
public function getNamed(): ?string
{
return $this->named;
}
/**
* @param string $named
*/
public function setNamed(string $named): void
{
$this->named = $named;
}
/**
* @return string
*/
public function getSurname(): ?string
{
return $this->surname;
}
/**
* @param string $surname
*/
public function setSurname(string $surname): void
{
$this->surname = $surname;
}
/**
* @return string
*/
public function getBuilding(): ?string
{
return $this->building;
}
/**
* @param string $building
*/
public function setBuilding(string $building): void
{
$this->building = $building;
}
/**
* @return string
*/
public function getStreet(): ?string
{
return $this->street;
}
/**
* @param string $street
*/
public function setStreet(string $street): void
{
$this->street = $street;
}
/**
* @return string
*/
public function getOther(): ?string
{
return $this->other;
}
/**
* @param string $other
*/
public function setOther(string $other): void
{
$this->other = $other;
}
/**
* @return int
*/
public function getZipcode(): ?int
{
return $this->zipcode;
}
/**
* @param int $zipcode
*/
public function setZipcode(int $zipcode): void
{
$this->zipcode = $zipcode;
}
/**
* @return string
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @param string $city
*/
public function setCity(string $city): void
{
$this->city = $city;
}
/**
* @return string
*/
public function getCountry(): ?string
{
return $this->country;
}
/**
* @param string $country
*/
public function setCountry(string $country): void
{
$this->country = $country;
}
/**
* @return string
*/
public function getUrl(): ?string
{
return $this->url;
}
/**
* @param string $url
*/
public function setUrl(string $url): void
{
$this->url = $url;
}
/**
* @return string
*/
public function getLogin(): ?string
{
return $this->login;
}
/**
* @param string $login
*/
public function setLogin(string $login): void
{
$this->login = $login;
}
/**
* @return string
*/
public function getPasswordSite(): ?string
{
return $this->passwordSite;
}
/**
* @param string $passwordSite
*/
public function setPasswordSite(string $passwordSite): void
{
$this->passwordSite = $passwordSite;
}
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $imageFile
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getPhone1(): ?string
{
return $this->phone1;
}
public function setPhone1(string $phone1): self
{
$this->phone1 = $phone1;
return $this;
}
public function getPhone2(): ?string
{
return $this->phone2;
}
public function setPhone2(string $phone2): self
{
$this->phone2 = $phone2;
return $this;
}
public function getFax(): ?string
{
return $this->fax;
}
public function setFax(string $fax): self
{
$this->fax = $fax;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(string $note): self
{
$this->note = $note;
return $this;
}
}
如果要基于另一个字段修改处理,可以使用FormEvents
$builder
->add('reference')
->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) use ($options) {
// if fournisseurs is set, then do logic
if (isset(($event->getData())['fournisseurs'])) {
$form = $event->getForm();
// do whatever you need with this field
$form->add('reference');
}
}
);
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ReferenceRepository")
*/
class Reference
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $refcode;
/**
* @ORM\OneToOne(targetEntity="Article", cascade={"remove"})
*/
private $article;
/**
* @ORM\ManyToOne(targetEntity="Fournisseur", inversedBy="references")
* @ORM\JoinColumn(name="fournisseur_id", referencedColumnName="id")
*/
private $fournisseur;
public function __toString()
{
return $this->refcode;
}
public function getId(): ?int
{
return $this->id;
}
public function getRefcode(): ?string
{
return $this->refcode;
}
public function setRefcode(string $refcode): self
{
$this->refcode = $refcode;
return $this;
}
public function getArticle()
{
return $this->article;
}
public function setArticle($article): void
{
$this->article = $article;
}
}
<?php
namespace App\Form;
use App\Entity\Article;
use App\Entity\Fournisseur;
use App\Entity\Reference;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
class ArticlesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id_tactill', HiddenType::class)
->add('name', TextType::class, [
'label' => 'Nom'
])
->add('price', MoneyType::class, [
'label' => 'Prix'
])
->add('reference')
->add('in_stock', CheckboxType::class, [
'label' => 'En stock'
])
->add('image')
->add('taxes', HiddenType::class)
->add('fournisseurs', EntityType::class, [
'class' => Fournisseur::class,
'label' => 'Choisir un fournisseur',
'required' => false,
'attr' => ['class' => 'js_fournisseur'],
'choice_label' => function ($fournisseur) {
return $fournisseur->getTitle();
}
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Article::class,
]);
}
}
$builder
->add('reference')
->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) use ($options) {
// if fournisseurs is set, then do logic
if (isset(($event->getData())['fournisseurs'])) {
$form = $event->getForm();
// do whatever you need with this field
$form->add('reference');
}
}
);