Because of the non-intrusive design of Doctrine, it is possible for you to have plain PHP classes that are mapped to both a relational database (with the Doctrine2 Object Relational Mapper) and MongoDB (with the Doctrine MongoDB Object Document Mapper), or any other persistence layer that implements the Doctrine Persistence persistence interfaces.
For this cookbook entry, we need to define a class that can be persisted to both MySQL and MongoDB.
We'll use a BlogPost
as you may want to write some generic blogging functionality that has support
for multiple Doctrine persistence layers:
<?php
namespace Documents\Blog;
class BlogPost
{
private $id;
private $title;
private $body;
// ...
}
Now we just need to provide the mapping information for the Doctrine persistence layers so they know how to consume the objects and persist them to the database.
First define the mapping for the ORM:
.. configuration-block:: .. code-block:: php <?php namespace Documents\Blog; use Documents\Blog\Repository\ORM\BlogPostRepository; /** @Entity(repositoryClass=BlogPostRepository::class) */ class BlogPost { /** @Id @Column(type="int") */ private $id; /** @Column(type="string") */ private $title; /** @Column(type="text") */ private $body; // ... } .. code-block:: xml <?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="Documents\Blog\BlogPost" repository-class="Documents\Blog\Repository\ORM\BlogPostRepository"> <id name="id" type="int" /> <field name="name" type="string" /> <field name="email" type="text" /> </entity> </doctrine-mapping>
Now you are able to persist the Documents\Blog\BlogPost
with an instance of EntityManager
:
<?php
$blogPost = new BlogPost();
$blogPost->setTitle('test');
$em->persist($blogPost);
$em->flush();
You can find the blog post:
<?php
$blogPost = $em->getRepository(BlogPost::class)->findOneBy(array('title' => 'test'));
Now map the same class to the Doctrine MongoDB ODM:
.. configuration-block:: .. code-block:: php <?php namespace Documents\Blog; use Documents\Blog\Repository\ODM\BlogPostRepository; /** @Document(repositoryClass=BlogPostRepository::class) */ class BlogPost { /** @Id */ private $id; /** @Field(type="string") */ private $title; /** @Field(type="string") */ private $body; // ... } .. code-block:: xml <?xml version="1.0" encoding="UTF-8"?> <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <document name="Documents\Blog\BlogPost" repository-class="Documents\Blog\Repository\ODM\BlogPostRepository"> <id strategy="INCREMENT" type="int" /> <field field-name="name" type="string" /> <field field-name="email" type="text" /> </document> </doctrine-mongo-mapping>
Now the same class is able to be persisted in the same way using an instance of DocumentManager
:
<?php
$blogPost = new BlogPost();
$blogPost->setTitle('test');
$dm->persist($blogPost);
$dm->flush();
You can find the blog post:
<?php
$blogPost = $dm->getRepository(BlogPost::class)->findOneBy(array('title' => 'test'));
You can implement the same repository interface for the ORM and MongoDB ODM easily, e.g. by creating BlogPostRepositoryInterface
:
<?php
// An Interface to ensure ORM and ODM Repository classes have the same methods implemented
namespace Documents\Blog\Repository;
use Documents\Blog\BlogPost;
interface BlogPostRepositoryInterface
{
public function findPostById(int $id): ?BlogPost;
}
Define repository methods required by the interface for the ORM:
<?php
namespace Documents\Blog\Repository\ORM;
use Documents\Blog\Repository\BlogPostRepositoryInterface;
use Doctrine\ORM\EntityRepository;
class BlogPostRepository extends EntityRepository implements BlogPostRepositoryInterface
{
public function findPostById(int $id): ?BlogPost
{
return $this->findOneBy(['id' => $id]);
}
}
Now define the same repository methods for the MongoDB ODM:
<?php
namespace Documents\Blog\Repository\ODM;
use Documents\Blog\Repository\BlogPostRepositoryInterface;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
class BlogPostRepository extends DocumentRepository implements BlogPostRepositoryInterface
{
public function findPostById(int $id): ?BlogPost
{
return $this->findOneBy(['id' => $id]);
}
}
As you can see the repositories are the same and the final returned data is the same vanilla PHP objects. The data is transparently injected to the objects for you automatically so you are not forced to extend some base class or shape your domain in any certain way for it to work with the Doctrine persistence layers.