Skip to content

Convert MongoDB tutorial to PHP Attributes #2079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-community-server:latest
environment:
- MONGO_INITDB_DATABASE=api
- MONGO_INITDB_ROOT_USERNAME=api-platform
- MONGODB_INITDB_DATABASE=api
- MONGODB_INITDB_ROOT_USERNAME=api-platform
Comment on lines -54 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing legacy env vars from the mongo docker image, with the new env vars that works with mongodb/mongodb-atlas-local and mongodb/mongodb-community-server.

# 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']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# ...
```

## 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding prop types.


#[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