lumen-doctrine-mongodb-odm icon indicating copy to clipboard operation
lumen-doctrine-mongodb-odm copied to clipboard

Help, how to use this?

Open ghost opened this issue 9 years ago • 4 comments

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

ghost avatar Nov 21 '16 10:11 ghost

Hi @sasavranic,

here are some examples to set up your project for ODM:

  1. In config/odm.php you need to set the paths to your model declarations:
 'mapping' => 'yaml',
 'paths' => [
        base_path('app/path/to/ODM'),
        ...
    ],
  1. in config/mongodb.php you 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.

  1. In your bootstrap/app.php you need to register the service provider:
$app->configure('odm');
$app->configure('mongodb');
$app->register(Nord\Lumen\Doctrine\ODM\MongoDB\DoctrineServiceProvider::class);
  1. 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
...
  1. Extend your repositories from Nord\Lumen\Doctrine\ODM\MongoDB\Infrastructure\DocumentRepository
  2. Your models can use traits from Nord\Lumen\Doctrine\ODM\MongoDB\Traits such 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 avatar Nov 24 '16 09:11 soderluk

@soderluk you think you could make a PR where this information is added to the README?

Jalle19 avatar Nov 24 '16 09:11 Jalle19

@Jalle19: sure, will do when I have time.

soderluk avatar Nov 24 '16 09:11 soderluk

Re-opening so it's not forgotten :)

hugovk avatar Nov 24 '16 09:11 hugovk