lumen-doctrine-mongodb-odm
lumen-doctrine-mongodb-odm copied to clipboard
Help, how to use this?
Hi, This is not the issue. I just wanted to know, can you provide some examples for using this with Lumen, controllers etc... I'm totally new with Doctrine. Until now I worked mostly with Eloquent and Laravel, or Rails and Mongo. Browsed Doctrine manual, found some examples, but I have no idea how to initiate DocumentManager through complete project and use models. If I try to init DocumentManger it asks me for connection and configuration, but configuration is already registered with config/odm.php and config/mongodb.php files. Thanks
Hi @sasavranic,
here are some examples to set up your project for ODM:
- In
config/odm.phpyou need to set the paths to your model declarations:
'mapping' => 'yaml',
'paths' => [
base_path('app/path/to/ODM'),
...
],
- in
config/mongodb.phpyou need to set your configuration for the connection:
return [
'default' => 'mongodb',
'connections' => [
'mongodb' => [
'host' => 'mongodb://USERNAME:PASSWORD@localhost:27017',
'database' => env('MONGODB_DB_DATABASE', 'forge'),
'username' => env('MONGODB_DB_USERNAME', 'forge'),
'password' => env('MONGODB_DB_PASSWORD', ''),
'timezone' => env('MONGODB_DB_TIMEZONE', '+00:00'),
],
],
];
Note that the host-part includes the username and password. This is needed if the authentication is on for mongodb. These values should be taken from the env(), but they were removed now for readability.
- In your
bootstrap/app.phpyou need to register the service provider:
$app->configure('odm');
$app->configure('mongodb');
$app->register(Nord\Lumen\Doctrine\ODM\MongoDB\DoctrineServiceProvider::class);
- In the model declaration files, be it YAML or XML, you define them as documents:
App\Namespace\To\Person:
type: document
db: database_name
collection: persons
repositoryClass: App\Namespace\To\PersonRepository
fields:
id:
type: id
id: true
strategy: AUTO
domainId:
type: domain_id
name: domain_id
unique: true
name:
type: string
email:
type: string
...
- Extend your repositories from
Nord\Lumen\Doctrine\ODM\MongoDB\Infrastructure\DocumentRepository - Your models can use traits from
Nord\Lumen\Doctrine\ODM\MongoDB\Traitssuch as HasIdentity which has setters and getters for the DomainId.
use Nord\Lumen\Doctrine\ODM\MongoDB\Traits\HasIdentity;
class Person {
use HasIdentity;
/** @var \MongoId */
private $id;
private $name;
private $email;
public function __construct(DomainId $domainId, $name, $email)
{
$this->setDomainId($domainId);
$this->setName($name);
$this->setEmail($email);
}
public function getId()
{
return $this->id;
}
...
}
That's about it! Now you can use the repository to find models from MongoDB, create a service that handles creating and updating of the models. By using two traits CreatesIdentities and ManagesDocuments you have everything you need.
Here's a small example:
use Nord\Lumen\Doctrine\ODM\MongoDB\Traits\CreatesIdentities;
use Nord\Lumen\Doctrine\ODM\MongoDB\Traits\ManagesDocuments;
class PersonService {
use CreatesIdentities;
use ManagesDocuments;
public function createPerson($name, $email)
{
// Callback is to check that the domainId will be unique.
$domainId = $this->createDomainId(function ($value) {
return $this->getRepository()->domainIdExists($value);
} );
$person = new Person($domainId, $name, $email);
$this->saveDocumentAndCommit($person);
return $person;
}
private function getRepository()
{
return $this->getDocumentRepository(Person::class);
}
}
I hope this helps!
@soderluk you think you could make a PR where this information is added to the README?
@Jalle19: sure, will do when I have time.
Re-opening so it's not forgotten :)