Skip to content

Commit 16881ee

Browse files
authored
Convert MongoDB tutorial to PHP Attributes (#2079)
* Convert MongoDB tutorial to PHP Attributes api-platform/doctrine-odm is required but mapping.paths automatically contains ODM Document dir * Switch to mongodb-community-server
1 parent 8115686 commit 16881ee

File tree

2 files changed

+31
-70
lines changed

2 files changed

+31
-70
lines changed

core/getting-started.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class Product // The class name will be used to name exposed resources
7979

8080
/**
8181
* A name property - this description will be available in the API documentation too.
82-
*
8382
*/
8483
#[ORM\Column]
8584
#[Assert\NotBlank]
@@ -88,7 +87,6 @@ class Product // The class name will be used to name exposed resources
8887
// Notice the "cascade" option below, this is mandatory if you want Doctrine to automatically persist the related entity
8988
/**
9089
* @var Offer[]|ArrayCollection
91-
*
9290
*/
9391
#[ORM\OneToMany(targetEntity: Offer::class, mappedBy: 'product', cascade: ['persist'])]
9492
public iterable $offers;

core/mongodb.md

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ services:
4949
# ...
5050
db-mongodb:
5151
# In production, you may want to use a managed database service
52-
image: mongo
52+
image: mongodb/mongodb-community-server:latest
5353
environment:
54-
- MONGO_INITDB_DATABASE=api
55-
- MONGO_INITDB_ROOT_USERNAME=api-platform
54+
- MONGODB_INITDB_DATABASE=api
55+
- MONGODB_INITDB_ROOT_USERNAME=api-platform
5656
# You should definitely change the password in production
57-
- MONGO_INITDB_ROOT_PASSWORD=!ChangeMe!
57+
- MONGODB_INITDB_ROOT_PASSWORD=!ChangeMe!
5858
volumes:
5959
- db-data:/data/db:rw
6060
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
@@ -64,11 +64,10 @@ services:
6464
# ...
6565
```
6666

67-
In all cases, enable the MongoDB support by requiring the [Doctrine MongoDB ODM bundle](https://github.com/doctrine/DoctrineMongoDBBundle)
68-
package using Composer:
67+
In all cases, enable the MongoDB support by requiring the [Doctrine MongoDB ODM bundle](https://github.com/doctrine/DoctrineMongoDBBundle) and [MongoDB ODM for API Platform](https://github.com/api-platform/doctrine-odm/) packages using Composer:
6968

7069
```console
71-
composer require doctrine/mongodb-odm-bundle
70+
composer require doctrine/mongodb-odm-bundle api-platform/doctrine-odm
7271
```
7372

7473
Execute the contrib recipe to have it already configured.
@@ -81,20 +80,6 @@ MONGODB_URL=mongodb://api-platform:!ChangeMe!@db-mongodb
8180
MONGODB_DB=api
8281
```
8382

84-
Change the configuration of API Platform to add the right mapping path:
85-
86-
```yaml
87-
# api/config/packages/api_platform.yaml
88-
api_platform:
89-
# ...
90-
91-
mapping:
92-
paths:
93-
['%kernel.project_dir%/src/Entity', '%kernel.project_dir%/src/Document']
94-
95-
# ...
96-
```
97-
9883
## Creating Documents
9984

10085
Creating resources mapped to MongoDB documents is as simple as creating entities:
@@ -107,30 +92,23 @@ namespace App\Document;
10792

10893
use ApiPlatform\Metadata\ApiResource;
10994
use Doctrine\Common\Collections\ArrayCollection;
95+
use Doctrine\Common\Collections\Collection;
11096
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
11197
use Symfony\Component\Validator\Constraints as Assert;
11298

113-
/**
114-
* @ODM\Document
115-
*/
99+
#[ODM\Document]
116100
#[ApiResource]
117101
class Product
118102
{
119-
/**
120-
* @ODM\Id(strategy="INCREMENT", type="int")
121-
*/
122-
private $id;
123-
124-
/**
125-
* @ODM\Field
126-
*/
103+
#[ODM\Id(strategy: 'INCREMENT')]
104+
private ?int $id;
105+
106+
#[ODM\Field]
127107
#[Assert\NotBlank]
128-
public $name;
108+
public string $name;
129109

130-
/**
131-
* @ODM\ReferenceMany(targetDocument=Offer::class, mappedBy="product", cascade={"persist"}, storeAs="id")
132-
*/
133-
public $offers;
110+
#[ODM\ReferenceMany(targetDocument: Offer::class, mappedBy: 'product', cascade: ['persist'], storeAs: 'id')]
111+
public Collection $offers;
134112

135113
public function __construct()
136114
{
@@ -168,34 +146,23 @@ use ApiPlatform\Metadata\ApiResource;
168146
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
169147
use Symfony\Component\Validator\Constraints as Assert;
170148

171-
/**
172-
* @ODM\Document
173-
*/
149+
#[ODM\Document]
174150
#[ApiResource(types: ['https://schema.org/Offer'])]
175151
class Offer
176152
{
177-
/**
178-
* @ODM\Id(strategy="INCREMENT", type="int")
179-
*/
180-
private $id;
181-
182-
/**
183-
* @ODM\Field
184-
*/
185-
public $description;
186-
187-
/**
188-
* @ODM\Field(type="float")
189-
* @Assert\NotBlank
190-
* @Assert\Range(min=0, minMessage="The price must be superior to 0.")
191-
* @Assert\Type(type="float")
192-
*/
193-
public $price;
194-
195-
/**
196-
* @ODM\ReferenceOne(targetDocument=Product::class, inversedBy="offers", storeAs="id")
197-
*/
198-
public $product;
153+
#[ODM\Id(strategy: 'INCREMENT', type: 'int')]
154+
private int $id;
155+
156+
#[ODM\Field]
157+
public string $description;
158+
159+
#[ODM\Field(type: 'float')]
160+
#[Assert\Range(min: 0, minMessage: 'The price must be superior to 0.')]
161+
#[Assert\Type(type: 'float')]
162+
public float $price;
163+
164+
#[ODM\ReferenceOne(targetDocument: Product::class, inversedBy: 'offers', storeAs: 'id')]
165+
public ?Product $product;
199166

200167
public function getId(): ?int
201168
{
@@ -234,9 +201,7 @@ use ApiPlatform\Metadata\ApiResource;
234201
use ApiPlatform\Metadata\GetCollection;
235202
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
236203

237-
/**
238-
* @ODM\Document
239-
*/
204+
#[ODM\Document]
240205
#[ApiResource]
241206
#[GetCollection(extraProperties: ['doctrineMongodb' => ['execute_options' => ['allowDiskUse' => true]]])]
242207
class Offer
@@ -256,9 +221,7 @@ namespace App\Document;
256221
use ApiPlatform\Metadata\ApiResource;
257222
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
258223

259-
/**
260-
* @ODM\Document
261-
*/
224+
#[ODM\Document]
262225
#[ApiResource(extraProperties: ['doctrineMongodb' => ['execute_options' => ['allowDiskUse' => true]]])]
263226
class Offer
264227
{

0 commit comments

Comments
 (0)