I am trying to recover datas and modify them according to a selector. This is how my application works: I have vendors associated with articles, several vendors can have the same product except that the reference is different depending on the vendor and I am looking for the possibility of being able to modify this reference depending on the vendor.
Entity supplier:
<?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;
}
}
Entity article:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticlesRepository")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $id_tactill;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $price;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $reference;
/**
* @ORM\Column(type="boolean")
*/
private $in_stock;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $taxes;
/**
* Many Users have Many Groups.
* @ORM\ManyToMany(targetEntity="Fournisseur")
* @ORM\JoinTable(name="articles_fournisseurs",
* joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="fournisseur_id", referencedColumnName="id")}
* )
*/
private $fournisseurs;
public function getId(): ?int
{
return $this->id;
}
/**
* @return mixed
*/
public function getIdTactill()
{
return $this->id_tactill;
}
/**
* @param mixed $id_tactill
*/
public function setIdTactill($id_tactill): void
{
$this->id_tactill = $id_tactill;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getInStock(): ?bool
{
return $this->in_stock;
}
public function setInStock(bool $in_stock): self
{
$this->in_stock = $in_stock;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): self
{
$this->image = $image;
return $this;
}
public function getTaxes()
{
return $this->taxes;
}
public function setTaxes($taxes): void
{
$this->taxes = $taxes;
}
public function getFournisseurs()
{
return $this->fournisseurs;
}
public function setFournisseurs($fournisseurs): void
{
$this->fournisseurs = $fournisseurs;
}
}
Entity 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;
}
}
FormType 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,
]);
}
}
I block the design of the form that will allow me to display the reference according to the selection of the supplier in the article master and can modify it later.
Thank you for your help