Commit 0796ea9c authored by Mat's avatar Mat

form commande, première imbrication des légumes dans le form >>

voir https://symfony.com/doc/3.4/form/form_collections.html
parent fd950c65
......@@ -3,6 +3,7 @@
namespace PotageBundle\Controller;
use PotageBundle\Entity\Commande;
use PotageBundle\Entity\CommandeLegumes;
use PotageBundle\Entity\Groupe;
use PotageBundle\Entity\Lettre;
use PotageBundle\Entity\Offre;
......@@ -91,6 +92,18 @@ class MembreController extends MasterController
}
$commande = new Commande($offre, $user);
// dummy
$legume1 = new CommandeLegumes();
$legume1
->setPrixUnitaire(2.6)
->setUnite('botte')
->setQuantite(3)
->setPrix(7.8);
$commande->addCommandeLegumes($legume1);
// end dummy
$form = $this->createForm(CommandeType::class, $commande);
$form->handleRequest($request);
......
......@@ -2,6 +2,7 @@
namespace PotageBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
......@@ -38,7 +39,7 @@ class Commande
/**
* @var CommandeLegumes[]
* @ORM\OneToMany(targetEntity="PotageBundle\Entity\CommandeLegumes", mappedBy="commande")
* @ORM\OneToMany(targetEntity="PotageBundle\Entity\CommandeLegumes", mappedBy="commande", cascade={"persist"})
*/
private $commandeLegumes;
......@@ -87,10 +88,11 @@ class Commande
* @param Offre $offre
* @param User $user
*/
public function __construct($offre, $user)
public function __construct(Offre $offre, User $user)
{
$this->offre = $offre;
$this->user = $user;
$this->commandeLegumes = new ArrayCollection();
}
/**
......@@ -181,8 +183,11 @@ class Commande
return $this;
}
/**
* @return CommandeLegumes[]
* Get commandeLegumes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCommandeLegumes()
{
......@@ -190,15 +195,31 @@ class Commande
}
/**
* @param CommandeLegumes[] $commandeLegumes
* Add commandeLegumes
*
* @param \PotageBundle\Entity\CommandeLegumes $legume
*
* @return Commande
*/
public function setCommandeLegumes($commandeLegumes)
public function addCommandeLegumes(CommandeLegumes $legume)
{
$this->commandeLegumes = $commandeLegumes;
$this->commandeLegumes[] = $legume;
return $this;
}
/**
* Remove commandeLegumes
*
* @param \PotageBundle\Entity\commandeLegumes $legume
*/
public function removeCommandeLegumes(CommandeLegumes $legume)
{
$this->commandeLegumes->removeElement($legume);
}
/**
* @return float
*/
......
<?php
namespace PotageBundle\Form\Membre;
use PotageBundle\Entity\CommandeLegumes;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CommandeLegumesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('prixUnitaire')
->add('unite')
->add('quantite')
->add('prix')
;
}
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('data_class', CommandeLegumes::class);
$resolver->setDefault('attr', array(
'class' => 'formulaire formulaire_commande',
));
}
public function getBlockPrefix()
{
return 'commande_legumes';
}
}
......@@ -4,6 +4,7 @@ namespace PotageBundle\Form\Membre;
use PotageBundle\Entity\Commande;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
......@@ -14,6 +15,10 @@ class CommandeType extends AbstractType
{
$builder
->add('prixTotal')
->add('commandeLegumes', CollectionType::class, array(
'entry_type' => CommandeLegumesType::class,
'entry_options' => array('label' => false),
))
->add('sauver', SubmitType::class, array(
'label' => 'Enregistrer',
'attr' => array('class' => 'btn btn-dark mb-2')
......
......@@ -39,4 +39,15 @@
{% endfor %}
{% endfor %}
{{ form(formCommande) }}
{{ form_start(formCommande) }}
{% for legume in formCommande.commandeLegumes %}
{{ form_row(legume.prixUnitaire) }}
{{ form_row(legume.unite) }}
{{ form_row(legume.quantite) }}
{{ form_row(legume.prix) }}
{% endfor %}
{{ form_row(formCommande.prixTotal) }}
{{ form_end(formCommande) }}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment