rosamarsky / laravel-doctrine-odm
Simple Laravel Doctrine2 ODM adapter
1.0.5
2021-09-13 13:45 UTC
Requires
- php: ^7.1|^8
- ext-mongodb: *
- doctrine/mongodb-odm: ^2
- illuminate/container: *
- nesbot/carbon: ^2|^3
README
Installation & Configuration
-
Install via composer:
composer require rosamarsky/laravel-doctrine-odm
; -
Make sure
ServiceProvider.php
is registered in your application. -
Publish
doctrine-odm.php
config file via commandphp artisan vendor:publish
orcp ./vendor/rosamarsky/laravel-doctrine-odm/config/doctrine-odm.php ./config/doctrine-odm.php
; -
Set .env variables for your mongodb connection:
MONGO_HOST
MONGO_PORT
MONGO_DB
MONGO_USER
MONGO_PASS
Notes
Only annotation driver is available
Example
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; /** @ODM\Document(collection="users") */ class User { /** @ODM\Id() */ private string $id; /** @ODM\Field(type="string") */ private string $name; /** @ODM\Field(type="string") */ private string $email; /** @ODM\Field(type="carbon") */ private Carbon $createdAt; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } }
class UserController extends AbstractController { private $manager; public function __construct(\Doctrine\ODM\MongoDB\DocumentManager $manager) { $this->manager = $manager; } public function store(Request $request): User { $user = new User('Roman Samarsky', 'rosamarsky@gmail.com'); $this->manager->persist($user); $this->manager->flush(); return $user; } }