|
| 1 | +# Doctrine MongoDB MakerBundle |
| 2 | + |
| 3 | +A Symfony MakerBundle extension to generate MongoDB ODM code. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +The MongoDB MakerBundle extends Symfony's MakerBundle to provide code generators tailored for MongoDB Object Document Mapper (ODM). It streamlines the process of creating MongoDB documents, repositories, and fixtures within your Symfony application. |
| 8 | + |
| 9 | +This bundle integrates seamlessly with: |
| 10 | +- Symfony 7.4+ |
| 11 | +- Doctrine MongoDB ODM 5.5+ |
| 12 | +- MongoDB PHP Driver 2.1+ |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Install the bundle via Composer: |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require doctrine/mongodb-maker-bundle |
| 20 | +``` |
| 21 | + |
| 22 | +Then, register the bundle in your `config/bundles.php`: |
| 23 | + |
| 24 | +```php |
| 25 | +return [ |
| 26 | + // ... |
| 27 | + Doctrine\Bundle\MongoDBMakerBundle\MongoDBMakerBundle::class => ['all' => true], |
| 28 | +]; |
| 29 | +``` |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +Once installed, the bundle provides Maker commands to generate MongoDB-specific code. |
| 34 | + |
| 35 | +### Available Commands |
| 36 | + |
| 37 | +Run `php bin/console list make` to see all available makers: |
| 38 | + |
| 39 | +```bash |
| 40 | +php bin/console list make |
| 41 | +``` |
| 42 | + |
| 43 | +The MongoDB MakerBundle provides the following commands: |
| 44 | +- `make:document`: create or update a MongoDB ODM document class |
| 45 | +- `make:document:index`: add an index (regular, unique, or search) to a document |
| 46 | + |
| 47 | +### Examples |
| 48 | + |
| 49 | +#### Create a MongoDB Document |
| 50 | + |
| 51 | +Generate a new MongoDB document with properties: |
| 52 | + |
| 53 | +```bash |
| 54 | +php bin/console make:document |
| 55 | +``` |
| 56 | + |
| 57 | +Follow the interactive prompts. Example output: |
| 58 | + |
| 59 | +``` |
| 60 | +Class name of the document to create or update (e.g. GoodUser): |
| 61 | +> Product |
| 62 | +
|
| 63 | +New property name (press <return> to stop adding fields): |
| 64 | +> name |
| 65 | + Field type (enter ? to see all types) [string]: |
| 66 | + > string |
| 67 | + Can this field be null in the database (nullable) (yes/no) [no]: |
| 68 | + > no |
| 69 | +
|
| 70 | +Add another property? Enter the property name (or press <return> to stop adding fields): |
| 71 | +> price |
| 72 | + Field type (enter ? to see all types) [string]: |
| 73 | + > float |
| 74 | + Can this field be null in the database (nullable) (yes/no) [no]: |
| 75 | + > no |
| 76 | +
|
| 77 | +Add another property? Enter the property name (or press <return> to stop adding fields): |
| 78 | + > |
| 79 | +``` |
| 80 | + |
| 81 | +This generates a `Product` document class in `src/Document/Product.php`: |
| 82 | + |
| 83 | +```php |
| 84 | +<?php |
| 85 | + |
| 86 | +namespace App\Document; |
| 87 | + |
| 88 | +use Doctrine\ODM\MongoDB\Mapping\Attribute as ODM; |
| 89 | + |
| 90 | +#[ODM\Document(collection: 'product', repositoryClass: ProductRepository::class)] |
| 91 | +class Product |
| 92 | +{ |
| 93 | + #[ODM\Id] |
| 94 | + public ?string $id = null; |
| 95 | + |
| 96 | + #[ODM\Field(type: 'string')] |
| 97 | + private string $name; |
| 98 | + |
| 99 | + #[ODM\Field(type: 'float')] |
| 100 | + private float $price; |
| 101 | + |
| 102 | + // Getters and setters... |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +You can also add relations (`ReferenceOne`, `ReferenceMany`, `EmbedOne`, `EmbedMany`) between documents by entering `relation` as the field type and following the prompts. |
| 107 | + |
| 108 | +Running `make:document` again on an existing document lets you add more fields to it. |
| 109 | + |
| 110 | +#### Add an Index to a Document |
| 111 | + |
| 112 | +Add a regular, unique, or search index to an existing document: |
| 113 | + |
| 114 | +```bash |
| 115 | +php bin/console make:document:index |
| 116 | +``` |
| 117 | + |
| 118 | +Follow the interactive prompts to select the document, the index type, and the fields to index. After adding an index, remember to run: |
| 119 | + |
| 120 | +```bash |
| 121 | +php bin/console doctrine:mongodb:schema:update |
| 122 | +``` |
| 123 | + |
| 124 | +to create the index in the database. |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +The MongoDB MakerBundle is licensed under the MIT License. See the LICENSE file for details. |
| 129 | + |
| 130 | +## Support |
| 131 | + |
| 132 | +For issues, feature requests, or questions, please open an issue on the GitHub repository or reach out to the Doctrine community. |
0 commit comments