I'm trying to recover some product and add a quantity field to the side for each of them. All of this, in a promo code's formular.
PromoCode formular without product/quantity form row:
class CodePromoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('montantMinimum', MoneyType::class, [
'label' => 'Montant minimum en € requis pour appliquer la promotion',
'attr' => ['placeholder' => '10']
])
->add('montant', TextType::class, [
'label' => 'Montant en € ou %',
'attr' => ['placeholder' => '10']
])
->add('DLC', DateType::class, [
'label' => 'Date limite de consomation',
'attr' => ['class' => 'form-control']
])
->add('fraisDePortOfferts', CheckboxType::class, [
'label' => 'Frais de port offert',
'required' => false,
'attr' => ['class' => 'form-control']
])
->add('typePromo', ChoiceType::class, [
'label' => 'Type de promotion',
'choices' => [
'Euro €' => 'e',
'Pourcentage %' => 'p',
],
'attr' => ['class' => 'form-control']
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => CodePromo::class,
'allow_extra_fields' => true
]);
}
}
I'm using the products's relation (oneToMany) in my PromoCode class to create my form with the form builder.
#[ORM\Entity(repositoryClass: CodePromoRepository::class)]
class CodePromo
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 20)]
private $code;
#[ORM\Column(type: 'datetime')]
private $DLC;
#[ORM\Column(type: 'datetime')]
private $dateC;
#[ORM\Column(type: 'float')]
private $montant;
#[ORM\Column(type: 'string', length: 1)]
private $typePromo;
#[ORM\Column(type: 'float')]
private $montantMinimum;
#[ORM\OneToMany(mappedBy: 'codePromo', targetEntity: ProduitCodePromo::class)]
private $produits;
#[ORM\Column(type: 'boolean')]
private $fraisDePortOfferts;
public function __construct()
{
$this->produits = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getDLC(): ?\DateTimeInterface
{
return $this->DLC;
}
public function setDLC(\DateTimeInterface $DLC): self
{
$this->DLC = $DLC;
return $this;
}
public function getDateC(): ?\DateTimeInterface
{
return $this->dateC;
}
public function setDateC(\DateTimeInterface $dateC): self
{
$this->dateC = $dateC;
return $this;
}
public function getMontant(): ?float
{
return $this->montant;
}
public function setMontant(float $montant): self
{
$this->montant = $montant;
return $this;
}
public function getTypePromo(): ?string
{
return $this->typePromo;
}
public function setTypePromo(string $typePromo): self
{
$this->typePromo = $typePromo;
return $this;
}
public function getMontantMinimum(): ?float
{
return $this->montantMinimum;
}
public function setMontantMinimum(float $montantMinimum): self
{
$this->montantMinimum = $montantMinimum;
return $this;
}
/**
* @return Collection<int, ProduitCodePromo>
*/
public function getProduit(): Collection
{
return $this->produits;
}
public function addProduit(ProduitCodePromo $produits): self
{
if (!$this->produits->contains($produits)) {
$this->produits[] = $produits;
$produits->setCodePromo($this);
}
return $this;
}
public function removeProduit(ProduitCodePromo $produits): self
{
if ($this->produits->removeElement($produits)) {
// set the owning side to null (unless already changed)
if ($produits->getCodePromo() === $this) {
$produits->setCodePromo(null);
}
}
return $this;
}
public function getFraisDePortOfferts(): ?bool
{
return $this->fraisDePortOfferts;
}
public function setFraisDePortOfferts(bool $fraisDePortOfferts): self
{
$this->fraisDePortOfferts = $fraisDePortOfferts;
return $this;
}
}
This is the table linked with PromoCode, as u can see, I want to use this quantity field.
#[ORM\Entity(repositoryClass: ProduitCodePromoRepository::class)]
class ProduitCodePromo
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: Produit::class, inversedBy: 'produitsCodePromo')]
#[ORM\JoinColumn(nullable: false)]
private $produit;
#[ORM\ManyToOne(targetEntity: CodePromo::class)]
#[ORM\JoinColumn(nullable: false)]
private $codePromo;
#[ORM\Column(type: 'integer')]
private $quantite;
public function getId(): ?int
{
return $this->id;
}
public function getProduit(): ?Produit
{
return $this->produit;
}
public function setProduit(?Produit $produit): self
{
$this->produit = $produit;
return $this;
}
public function getCodePromo(): ?CodePromo
{
return $this->codePromo;
}
public function setCodePromo(?CodePromo $codePromo): self
{
$this->codePromo = $codePromo;
return $this;
}
public function getQuantite(): ?int
{
return $this->quantite;
}
public function setQuantite(int $quantite): self
{
$this->quantite = $quantite;
return $this;
}
}
You'll have to create a new FormType
for your ProduitCodePromo
entity and add it to your base form. You'll have to also add a product
field in your new formType.
The way it should be added is described in a lot of other answers here. In that case you'll have to use the CollectionType
. You'll find an (with ManyToMany relationship) exemple here Symfony 5 Many to Many with extra fields form builder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.