Bonjour,
Je suis entrain de développer une application qui permet de saisir des commandes à l'aide de Symfony 2.6.
L'idée est qu'une commande est liée obligatoirement à une personne et peut être composée de 1 ou plusieurs produits.
4 entités pour cela :
Order (les commandes)
Customer (les clients)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 class Order { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToMany(targetEntity="XXXXX\OrderBundle\Entity\OrderProduct", mappedBy="order") */ private $orderProducts; /** * @ORM\Column(type="decimal", scale=2) */ private $totalPrice; /** * @ORM\Column(type="datetime") */ private $date; /** * @ORM\ManyToOne(targetEntity="XXXX\RhBundle\Entity\Customer", inversedBy="orders") * @ORM\JoinColumn(name="customer_id", referencedColumnName="id") */ protected $customer; //Les GETTERS et SETTERS }
Product (les produits)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 class Customer { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=5) */ protected $sexe; /** * @ORM\Column(type="string", length=100) */ protected $nom; /** * @ORM\Column(type="string", length=50) */ protected $prenom; /** * @ORM\Column(type="string", length=10) */ protected $tel; /** * @ORM\Column(type="string", length=50) */ protected $email; /** * @ORM\Column(type="string", length=50) */ protected $address; /** * @ORM\Column(type="string", length=5) */ protected $cp; /** * @ORM\Column(type="string", length=50) */ protected $city; /** * @ORM\OneToMany(targetEntity="XXXXX\OrderBundle\Entity\Order", mappedBy="customer") */ protected $orders; // les GETTERS et SETTERS }
OrderProduct (l'entité de liaison entre Order et Product)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 class Product { /** * @ORM\Column(name="reference", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ protected $reference; /** * @ORM\Column(type="string", length=100) */ protected $libelle; /** * @ORM\Column(type="decimal", scale=2) */ protected $price; /** * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ protected $category; /** * @ORM\OneToMany(targetEntity="XXXXX\OrderBundle\Entity\OrderProduct", mappedBy="product") */ private $orderProducts; // Les GETTERS et SETTERS }
J'ai créé deux formType:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 class OrderProduct { /** * @ORM\Id * @ORM\ManyToOne(targetEntity="XXXXX\OrderBundle\Entity\Order", inversedBy="orderProducts") * @ORM\JoinColumn(name="order_id", referencedColumnName="id") */ private $order; /** * @ORM\Id * @ORM\ManyToOne(targetEntity="XXXXX\ProductBundle\Entity\Product", inversedBy="orderProducts") * @ORM\JoinColumn(name="product_reference", referencedColumnName="reference") */ private $product; /** * @ORM\Column() */ private $quantity; //Les GETTERS et SETTERS }
OrderType
OrderProductType
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('totalPrice', 'text', array("label" => "XXXXXRhBundle.customers.page_new_edit.sexe")) ->add('date', 'date', array("label" => "XXXXXRhBundle.customers.page_new_edit.firstName")) ->add('customer','entity', array ('class' => 'XXXXX\RhBundle\Entity\Customer', 'empty_value' => 'Choisissez un client...', 'required' => true)) ->add('orderProducts', 'collection', array('type' => new OrderProductType(), 'allow_add' => true, 'allow_delete' => true )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Stanhome\OrderBundle\Entity\Order', 'cascade_validation' => true, )); }
Lors de la soumission du formulaire OrderType, il faut donc dans un premier temps créer une commande dans la base de données et juste après créer des lignes dans l'entité OrderProduct afin d'avoir pour une commande, plusieurs produits et la quantité pour chacun des produits.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('quantity', 'integer', array("label" => "XXXXXRhBundle.customers.page_new_edit.firstName")) ->add('product','entity', array ('class' => 'XXXXX\ProductBundle\Entity\Product', 'empty_value' => 'Choisissez un produit...', 'required' => true)); }
Hors j'ai un soucis avec handleRequest(), il me dit :
Neither the property "quantity" nor one of the methods "getQuantity()", "quantity()", "isQuantity()", "hasQuantity()", "__get()" exist and have public access in class "XXXXX\OrderBundle\Entity\Order".
C'est normal car la propriété quantity est dans l'entité OrderProduct, pas dans l'entité Order ! Donc là je coince, je ne vois pas comment faire les enregistrements l'un à la suite de l'autre. Merci de votre aide !
OrderController
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 public function createAction(Request $request) { $entity = new Order(); $form = $this->createCreateForm($entity); //fonction qui permet de générer le formulaire OrderType. $form->handleRequest($request); //Que faut-il écrire pour pouvoir enregistrer d'abord une commande ORDER et ensuite des lignes dans OrderProduct ???
Partager