Skip to content

Commit

Permalink
Convert MongoDB tutorial to PHP Attributes
Browse files Browse the repository at this point in the history
api-platform/doctrine-odm is required but mapping.paths automatically contains ODM Document dir
  • Loading branch information
GromNaN committed Nov 29, 2024
1 parent 8115686 commit abf0323
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 70 deletions.
2 changes: 0 additions & 2 deletions core/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class Product // The class name will be used to name exposed resources

/**
* A name property - this description will be available in the API documentation too.
*
*/
#[ORM\Column]
#[Assert\NotBlank]
Expand All @@ -88,7 +87,6 @@ class Product // The class name will be used to name exposed resources
// Notice the "cascade" option below, this is mandatory if you want Doctrine to automatically persist the related entity
/**
* @var Offer[]|ArrayCollection
*
*/
#[ORM\OneToMany(targetEntity: Offer::class, mappedBy: 'product', cascade: ['persist'])]
public iterable $offers;
Expand Down
99 changes: 31 additions & 68 deletions core/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ services:
# ...
db-mongodb:
# In production, you may want to use a managed database service
image: mongo
image: mongodb/mongodb-atlas-local:latest
environment:
- MONGO_INITDB_DATABASE=api
- MONGO_INITDB_ROOT_USERNAME=api-platform
- MONGODB_INITDB_DATABASE=api
- MONGODB_INITDB_ROOT_USERNAME=api-platform
# You should definitely change the password in production
- MONGO_INITDB_ROOT_PASSWORD=!ChangeMe!
- MONGODB_INITDB_ROOT_PASSWORD=!ChangeMe!
volumes:
- db-data:/data/db:rw
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
Expand All @@ -64,11 +64,10 @@ services:
# ...
```

In all cases, enable the MongoDB support by requiring the [Doctrine MongoDB ODM bundle](https://github.com/doctrine/DoctrineMongoDBBundle)
package using Composer:
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:

```console
composer require doctrine/mongodb-odm-bundle
composer require doctrine/mongodb-odm-bundle api-platform/doctrine-odm
```

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

Change the configuration of API Platform to add the right mapping path:

```yaml
# api/config/packages/api_platform.yaml
api_platform:
# ...

mapping:
paths:
['%kernel.project_dir%/src/Entity', '%kernel.project_dir%/src/Document']

# ...
```

## Creating Documents

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

use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ODM\Document
*/
#[ODM\Document]
#[ApiResource]
class Product
{
/**
* @ODM\Id(strategy="INCREMENT", type="int")
*/
private $id;

/**
* @ODM\Field
*/
#[ODM\Id(strategy: 'INCREMENT')]
private ?int $id;

#[ODM\Field]
#[Assert\NotBlank]
public $name;
public string $name;

/**
* @ODM\ReferenceMany(targetDocument=Offer::class, mappedBy="product", cascade={"persist"}, storeAs="id")
*/
public $offers;
#[ODM\ReferenceMany(targetDocument: Offer::class, mappedBy: 'product', cascade: ['persist'], storeAs: 'id')]
public Collection $offers;

public function __construct()
{
Expand Down Expand Up @@ -168,34 +146,23 @@ use ApiPlatform\Metadata\ApiResource;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ODM\Document
*/
#[ODM\Document]
#[ApiResource(types: ['https://schema.org/Offer'])]
class Offer
{
/**
* @ODM\Id(strategy="INCREMENT", type="int")
*/
private $id;

/**
* @ODM\Field
*/
public $description;

/**
* @ODM\Field(type="float")
* @Assert\NotBlank
* @Assert\Range(min=0, minMessage="The price must be superior to 0.")
* @Assert\Type(type="float")
*/
public $price;

/**
* @ODM\ReferenceOne(targetDocument=Product::class, inversedBy="offers", storeAs="id")
*/
public $product;
#[ODM\Id(strategy: 'INCREMENT', type: 'int')]
private int $id;

#[ODM\Field]
public string $description;

#[ODM\Field(type: 'float')]
#[Assert\Range(min: 0, minMessage: 'The price must be superior to 0.')]
#[Assert\Type(type: 'float')]
public float $price;

#[ODM\ReferenceOne(targetDocument: Product::class, inversedBy: 'offers', storeAs: 'id')]
public ?Product $product;

public function getId(): ?int
{
Expand Down Expand Up @@ -234,9 +201,7 @@ use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\Document
*/
#[ODM\Document]
#[ApiResource]
#[GetCollection(extraProperties: ['doctrineMongodb' => ['execute_options' => ['allowDiskUse' => true]]])]
class Offer
Expand All @@ -256,9 +221,7 @@ namespace App\Document;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\Document
*/
#[ODM\Document]
#[ApiResource(extraProperties: ['doctrineMongodb' => ['execute_options' => ['allowDiskUse' => true]]])]
class Offer
{
Expand Down

0 comments on commit abf0323

Please sign in to comment.