@@ -44,37 +44,39 @@ use Doctrine\Common\Collections\ArrayCollection;
44
44
use Symfony\Component\Validator\Constraints as Assert;
45
45
46
46
/**
47
- * @ApiResource
48
47
* @ORM\Entity
49
48
*/
49
+ #[ApiResource]
50
50
class Product // The class name will be used to name exposed resources
51
51
{
52
52
/**
53
- * @ORM\Column(type="integer")
54
53
* @ORM\Id
55
54
* @ORM\GeneratedValue(strategy="AUTO")
55
+ * @ORM\Column(type="integer")
56
56
*/
57
- private $id;
57
+ private ?int $id = null ;
58
58
59
59
/**
60
- * @var string $name A name property - this description will be available in the API documentation too.
60
+ * A name property - this description will be available in the API documentation too.
61
61
*
62
62
* @ORM\Column
63
- * @Assert\NotBlank
64
63
*/
65
- public $name;
64
+ #[Assert\NotBlank]
65
+ public string $name = '';
66
66
67
67
// Notice the "cascade" option below, this is mandatory if you want Doctrine to automatically persist the related entity
68
68
/**
69
+ * @var Offer[]|ArrayCollection
70
+ *
69
71
* @ORM\OneToMany(targetEntity="Offer", mappedBy="product", cascade={"persist"})
70
72
*/
71
- public $offers;
73
+ public iterable $offers;
72
74
73
75
public function __construct()
74
76
{
75
77
$this->offers = new ArrayCollection(); // Initialize $offers as a Doctrine collection
76
78
}
77
-
79
+
78
80
public function getId(): ?int
79
81
{
80
82
return $this->id;
@@ -93,7 +95,7 @@ class Product // The class name will be used to name exposed resources
93
95
$offer->product = null;
94
96
$this->offers->removeElement($offer);
95
97
}
96
-
98
+
97
99
// ...
98
100
}
99
101
```
@@ -111,35 +113,33 @@ use Symfony\Component\Validator\Constraints as Assert;
111
113
/**
112
114
* An offer from my shop - this description will be automatically extracted from the PHPDoc to document the API.
113
115
*
114
- * @ApiResource(iri="http://schema.org/Offer")
115
116
* @ORM\Entity
116
117
*/
118
+ #[ApiResource(iri: 'http://schema.org/Offer')]
117
119
class Offer
118
120
{
119
121
/**
120
122
* @ORM\Column(type="integer")
121
123
* @ORM\Id
122
124
* @ORM\GeneratedValue(strategy="AUTO")
123
125
*/
124
- private $id;
126
+ private ?int $id = null ;
125
127
126
128
/**
127
129
* @ORM\Column(type="text")
128
130
*/
129
- public $description;
131
+ public string $description = '' ;
130
132
131
133
/**
132
134
* @ORM\Column(type="float")
133
- * @Assert\NotBlank
134
- * @Assert\Range(min=0, minMessage="The price must be superior to 0.")
135
- * @Assert\Type(type="float")
136
135
*/
137
- public $price;
136
+ #[Assert\Range(minMessage: 'The price must be superior to 0.', min: 0)]
137
+ public float $price = -1.0;
138
138
139
139
/**
140
140
* @ORM\ManyToOne(targetEntity="Product", inversedBy="offers")
141
141
*/
142
- public $product;
142
+ public ?Product $product = null ;
143
143
144
144
public function getId(): ?int
145
145
{
0 commit comments