-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
# 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! | ||
|
@@ -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. | ||
|
@@ -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'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This configuration is not necessary as is it by default. |
||
|
||
# ... | ||
``` | ||
|
||
## Creating Documents | ||
|
||
Creating resources mapped to MongoDB documents is as simple as creating entities: | ||
|
@@ -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() | ||
{ | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
{ | ||
|
@@ -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 | ||
|
@@ -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 | ||
{ | ||
|
There was a problem hiding this comment.
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 withmongodb/mongodb-atlas-local
andmongodb/mongodb-community-server
.