View difference between Paste ID: f1f065cb0 and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | <?php |
2 | ||
3 | namespace Entity; | |
4 | ||
5 | /** | |
6 | * @Entity | |
7 | * @Table(name="user") | |
8 | */ | |
9 | class User | |
10 | { | |
11 | /** | |
12 | * @Id | |
13 | * @Column(name="id", type="integer") | |
14 | * @GeneratedValue(strategy="AUTO") | |
15 | */ | |
16 | protected $id; | |
17 | ||
18 | /** | |
19 | * @Column(name="name", type="string") | |
20 | */ | |
21 | public $name; | |
22 | ||
23 | /** | |
24 | * @OneToMany(targetEntity="Entity\Address", mappedBy="User", cascade={"persist", "remove"}) | |
25 | */ | |
26 | protected $Addresses; | |
27 | ||
28 | public function __construct() | |
29 | { | |
30 | $this->Addresses = new \Doctrine\Common\Collections\ArrayCollection(); | |
31 | } | |
32 | ||
33 | public function getId() { return $this->id; } | |
34 | ||
35 | public function getAddresses() { return $this->Addresses; } | |
36 | ||
37 | public function setAddresses(ArrayCollection $value) { $this->Addresses = $value; } | |
38 | } | |
39 | ||
40 | /** | |
41 | * @Entity | |
42 | * @Table(name="address") | |
43 | */ | |
44 | class Address | |
45 | { | |
46 | /** | |
47 | * @Id | |
48 | * @Column(name="id", type="integer") | |
49 | * @GeneratedValue(strategy="AUTO") | |
50 | */ | |
51 | protected $id; | |
52 | ||
53 | /** | |
54 | * @Column(name="addresse", type="string") | |
55 | */ | |
56 | public $address; | |
57 | ||
58 | /** | |
59 | * @ManyToOne(targetEntity="Entity\User") | |
60 | * @JoinColumn(name="userId", referencedColumnName="id") | |
61 | */ | |
62 | protected $User; | |
63 | ||
64 | ||
65 | public function getId() { return $this->id; } | |
66 | ||
67 | public function setUser(User $user) { $this->User = $user; } | |
68 | ||
69 | public function getUser() { return $this->User; } | |
70 | } | |
71 | ||
72 | ||
73 | ## Test code | |
74 | ||
75 | $em = EntityManager::create($connectionOptions, $config); | |
76 | ||
77 | $id = 32; // User 32 exists and has addresses (previous run of this script!) | |
78 | ||
79 | /* | |
80 | $user = new \Entity\User; | |
81 | $user->name = 'Bob'; | |
82 | $em->persist($user); | |
83 | $em->flush(); | |
84 | ||
85 | $address = new \Entity\Address; | |
86 | $address->address = 'Streetname....'; | |
87 | $address->setUser($user); | |
88 | $em->persist($address); | |
89 | $em->flush(); | |
90 | ||
91 | $user->getAddresses()->add($address); | |
92 | $em->flush(); | |
93 | ||
94 | $id = $user->getId() | |
95 | */ | |
96 | ||
97 | $u = $em->find('Entity\User', $id); | |
98 | ||
99 | $addresses = $u->getAddresses(); | |
100 | ||
101 | @@foreach ($addresses as $a) { } // This line breaks! |