Symfony / doctrine insert related objects
ik heb een object "Order" en dat object heeft een array van orderDetails (classe: "OrderDetail").
Bij een nieuwe order wil ik zowel de order als de orderdetails in de database stoppen, en dat lukt perfect behalve de relatie tussen de twee objecten. Het "order_id" veld in de order_detail tabel blijft leeg. Hoe kan ik dat voorkomen?
Code waar ik de insert doe:
Dump van $order
Code (php)
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
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
object(Jds\ApiBundle\Entity\Order)[439]
protected 'id' => null
protected 'orderDate' =>
object(DateTime)[611]
public 'date' => string '2015-01-15 13:42:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)
protected 'orderDetails' =>
object(Doctrine\Common\Collections\ArrayCollection)[440]
private '_elements' =>
array (size=2)
0 =>
object(Jds\ApiBundle\Entity\OrderDetail)[667]
protected 'id' => null
protected 'product' =>
object(Jds\ApiBundle\Entity\Product)[736]
...
protected 'price' => float 1
protected 'amount' => int 2
protected 'order' => null
1 =>
object(Jds\ApiBundle\Entity\OrderDetail)[702]
protected 'id' => null
protected 'product' =>
object(Jds\ApiBundle\Entity\Product)[749]
...
protected 'price' => float 1
protected 'amount' => int 5
protected 'order' => null
protected 'id' => null
protected 'orderDate' =>
object(DateTime)[611]
public 'date' => string '2015-01-15 13:42:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)
protected 'orderDetails' =>
object(Doctrine\Common\Collections\ArrayCollection)[440]
private '_elements' =>
array (size=2)
0 =>
object(Jds\ApiBundle\Entity\OrderDetail)[667]
protected 'id' => null
protected 'product' =>
object(Jds\ApiBundle\Entity\Product)[736]
...
protected 'price' => float 1
protected 'amount' => int 2
protected 'order' => null
1 =>
object(Jds\ApiBundle\Entity\OrderDetail)[702]
protected 'id' => null
protected 'product' =>
object(Jds\ApiBundle\Entity\Product)[749]
...
protected 'price' => float 1
protected 'amount' => int 5
protected 'order' => null
Order-entity:
Code (php)
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
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
<?php
/**
* @ORM\Entity()
* @ORM\Table(name="the_order")
*/
class Order implements OrderInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=6)
* @ORM\GeneratedValue(strategy="AUTO")
* @Serializer\Groups({"orderList"})
*/
protected $id;
/**
* @ORM\Column(type="datetime", nullable=false)
* @Serializer\Groups({"orderList"})
*/
protected $orderDate;
//protected $orderedBy;
//protected $handledBy;
/**
* @ORM\OneToMany(targetEntity="OrderDetail", mappedBy="order")
* @Serializer\Groups({"orderList"})
**/
protected $orderDetails;
/**
* @ORM\Entity()
* @ORM\Table(name="the_order")
*/
class Order implements OrderInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=6)
* @ORM\GeneratedValue(strategy="AUTO")
* @Serializer\Groups({"orderList"})
*/
protected $id;
/**
* @ORM\Column(type="datetime", nullable=false)
* @Serializer\Groups({"orderList"})
*/
protected $orderDate;
//protected $orderedBy;
//protected $handledBy;
/**
* @ORM\OneToMany(targetEntity="OrderDetail", mappedBy="order")
* @Serializer\Groups({"orderList"})
**/
protected $orderDetails;
OrderDetail-entity
Code (php)
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
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
<?php
/**
* @ORM\Entity()
* @ORM\Table(name="order_detail")
*/
class OrderDetail
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=8)
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Product")
* @Serializer\Groups({"orderList"})
*/
protected $product;
/**
* @ORM\Column(type="decimal", precision=4, scale=2)
*/
protected $price;
/**
* @ORM\Column(type="integer", length=2)
* @Serializer\Groups({"orderList"})
*/
protected $amount;
/**
* @ORM\ManyToOne(targetEntity="Order", inversedBy="orderDetails")
**/
protected $order;
/**
* @ORM\Entity()
* @ORM\Table(name="order_detail")
*/
class OrderDetail
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=8)
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Product")
* @Serializer\Groups({"orderList"})
*/
protected $product;
/**
* @ORM\Column(type="decimal", precision=4, scale=2)
*/
protected $price;
/**
* @ORM\Column(type="integer", length=2)
* @Serializer\Groups({"orderList"})
*/
protected $amount;
/**
* @ORM\ManyToOne(targetEntity="Order", inversedBy="orderDetails")
**/
protected $order;
Gewijzigd op 15/01/2015 16:03:38 door Jasper DS
Er zijn nog geen reacties op dit bericht.