Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Jan 13, 2020
2 parents 0defe17 + 608d94f commit 697e550
Show file tree
Hide file tree
Showing 32 changed files with 136 additions and 46 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"doctrine/collections": "^1.5",
"doctrine/event-manager": "^1.0",
"doctrine/instantiator": "^1.1",
"doctrine/persistence": "^1.1",
"doctrine/persistence": "^1.3.4",
"mongodb/mongodb": "^1.2.0",
"ocramius/proxy-manager": "^2.2",
"symfony/console": "^3.4|^4.1|^5.0",
Expand Down
39 changes: 28 additions & 11 deletions docs/en/cookbook/blending-orm-and-mongodb-odm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Blending the ORM and MongoDB ODM

Since the start of the `Doctrine MongoDB Object Document Mapper`_ project people have asked how it can be integrated with the `ORM`_. This article will demonstrates how you can integrate the two transparently, maintaining a clean domain model.

This example will have a `Product` that is stored in MongoDB and the `Order` stored in a MySQL database.
This example will have a ``Product`` that is stored in MongoDB and the ``Order`` stored in a MySQL database.

Define Product
--------------

First lets define our `Product` document:
First lets define our ``Product`` document:

.. code-block:: php
Expand Down Expand Up @@ -44,7 +44,7 @@ First lets define our `Product` document:
Define Entity
-------------

Next create the `Order` entity that has a `$product` and `$productId` property linking it to the `Product` that is stored with MongoDB:
Next create the ``Order`` entity that has a ``$product`` and ``$productId`` property linking it to the ``Product`` that is stored with MongoDB:

.. code-block:: php
Expand Down Expand Up @@ -101,7 +101,7 @@ Next create the `Order` entity that has a `$product` and `$productId` property l
Event Subscriber
----------------

Now we need to setup an event subscriber that will set the `$product` property of all `Order` instances to a reference to the document product so it can be lazily loaded when it is accessed the first time. So first register a new event subscriber:
Now we need to setup an event subscriber that will set the ``$product`` property of all ``Order`` instances to a reference to the document product so it can be lazily loaded when it is accessed the first time. So first register a new event subscriber:

.. code-block:: php
Expand All @@ -112,7 +112,15 @@ Now we need to setup an event subscriber that will set the `$product` property o
[\Doctrine\ORM\Events::postLoad], new MyEventSubscriber($dm)
);
So now we need to define a class named `MyEventSubscriber` and pass a dependency to the `DocumentManager`. It will have a `postLoad()` method that sets the product document reference:
or in .yaml

.. code-block:: yaml
App\Listeners\MyEventSubscriber:
tags:
- { name: doctrine.event_listener, connection: default, event: postLoad}
So now we need to define a class named ``MyEventSubscriber`` and pass ``DocumentManager`` as a dependency. It will have a ``postLoad()`` method that sets the product document reference:

.. code-block:: php
Expand All @@ -131,22 +139,31 @@ So now we need to define a class named `MyEventSubscriber` and pass a dependency
public function postLoad(LifecycleEventArgs $eventArgs): void
{
$order = $eventArgs->getEntity();
if (!$order instanceof Order) {
return;
}
$em = $eventArgs->getEntityManager();
$productReflProp = $em->getClassMetadata('Entities\Order')
$productReflProp = $em->getClassMetadata(Order::class)
->reflClass->getProperty('product');
$productReflProp->setAccessible(true);
$productReflProp->setValue(
$order, $this->dm->getReference('Documents\Product', $order->getProductId())
$order, $this->dm->getReference(Product::class, $order->getProductId())
);
}
}
The `postLoad` method will be invoked after an ORM entity is loaded from the database. This allows us to use the `DocumentManager` to set the `$product` property with a reference to the `Product` document with the product id we previously stored.
The ``postLoad`` method will be invoked after an ORM entity is loaded from the database. This allows us
to use the ``DocumentManager`` to set the ``$product`` property with a reference to the ``Product`` document
with the product id we previously stored. Please note, that the event subscriber will be called on
postLoad for all entities that are loaded by doctrine. Thus, it is recommended to check for the current
entity.

Working with Products and Orders
--------------------------------

First create a new `Product`:
First create a new ``Product``:

.. code-block:: php
Expand All @@ -157,7 +174,7 @@ First create a new `Product`:
$dm->persist($product);
$dm->flush();
Now create a new `Order` and link it to a `Product` in MySQL:
Now create a new ``Order`` and link it to a ``Product`` in MySQL:

.. code-block:: php
Expand All @@ -180,7 +197,7 @@ Later we can retrieve the entity and lazily load the reference to the document i
echo "Order Title: " . $product->getTitle();
If you were to print the `$order` you would see that we got back regular PHP objects:
If you were to print the ``$order`` you would see that we got back regular PHP objects:

.. code-block:: php
Expand Down
4 changes: 2 additions & 2 deletions docs/en/cookbook/mapping-classes-to-orm-and-odm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Mapping Classes to the ORM and ODM
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 Common `persistence`_ interfaces.
implements the Doctrine Persistence `persistence`_ interfaces.

Test Subject
------------
Expand Down Expand Up @@ -226,4 +226,4 @@ PHP objects. The data is transparently injected to the objects for you automatic
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.

.. _persistence: https://github.com/doctrine/common/tree/master/lib/Doctrine/Common/Persistence
.. _persistence: https://github.com/doctrine/persistence
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Doctrine\ODM\MongoDB\Aggregation;

use BadMethodCallException;
use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use Doctrine\ODM\MongoDB\Types\Type;
use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
use LogicException;
use function array_map;
use function array_merge;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

use Doctrine\Common\Persistence\Mapping\MappingException as BaseMappingException;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Expr;
use Doctrine\ODM\MongoDB\Aggregation\Stage;
Expand All @@ -13,6 +12,7 @@
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use Doctrine\ODM\MongoDB\Types\Type;
use Doctrine\Persistence\Mapping\MappingException as BaseMappingException;
use LogicException;
use function array_map;
use function is_array;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

use Doctrine\Common\Persistence\Mapping\MappingException as BaseMappingException;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Stage;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use Doctrine\Persistence\Mapping\MappingException as BaseMappingException;

/**
* Fluent interface for building aggregation pipelines.
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Out.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

use Doctrine\Common\Persistence\Mapping\MappingException as BaseMappingException;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Stage;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\Persistence\Mapping\MappingException as BaseMappingException;

class Out extends Stage
{
Expand Down
7 changes: 5 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory;
Expand All @@ -20,12 +18,15 @@
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use ProxyManager\Configuration as ProxyManagerConfiguration;
use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use ReflectionClass;
use function interface_exists;
use function trim;

/**
Expand Down Expand Up @@ -475,3 +476,5 @@ public function getProxyManagerConfiguration() : ProxyManagerConfiguration
return $this->proxyManagerConfiguration;
}
}

interface_exists(MappingDriver::class);
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Doctrine\ODM\MongoDB;

use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
Expand All @@ -19,6 +17,8 @@
use Doctrine\ODM\MongoDB\Query\FilterCollection;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use MongoDB\Client;
use MongoDB\Collection;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Event/LifecycleEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Doctrine\ODM\MongoDB\Event;

use Doctrine\Common\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;
use function assert;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Doctrine\ODM\MongoDB\Event;

use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs as BaseLoadClassMetadataEventArgs;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs as BaseLoadClassMetadataEventArgs;
use function assert;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Event/ManagerEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Doctrine\ODM\MongoDB\Event;

use Doctrine\Common\Persistence\Event\ManagerEventArgs as BaseManagerEventArgs;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\Event\ManagerEventArgs as BaseManagerEventArgs;
use function assert;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Event/OnClearEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Doctrine\ODM\MongoDB\Event;

use Doctrine\Common\Persistence\Event\OnClearEventArgs as BaseOnClearEventArgs;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\Event\OnClearEventArgs as BaseOnClearEventArgs;
use function assert;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Doctrine\ODM\MongoDB\Mapping;

use BadMethodCallException;
use Doctrine\Common\Persistence\Mapping\ClassMetadata as BaseClassMetadata;
use Doctrine\Instantiator\Instantiator;
use Doctrine\Instantiator\InstantiatorInterface;
use Doctrine\ODM\MongoDB\Id\AbstractIdGenerator;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\Types\Type;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
use Doctrine\Persistence\Mapping\ClassMetadata as BaseClassMetadata;
use InvalidArgumentException;
use LogicException;
use ProxyManager\Proxy\GhostObjectInterface;
Expand Down
12 changes: 8 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
namespace Doctrine\ODM\MongoDB\Mapping;

use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\ReflectionService;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\ConfigurationException;
use Doctrine\ODM\MongoDB\DocumentManager;
Expand All @@ -20,11 +16,16 @@
use Doctrine\ODM\MongoDB\Id\AutoGenerator;
use Doctrine\ODM\MongoDB\Id\IncrementGenerator;
use Doctrine\ODM\MongoDB\Id\UuidGenerator;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\ReflectionService;
use ReflectionException;
use function assert;
use function get_class;
use function get_class_methods;
use function in_array;
use function interface_exists;
use function ucfirst;

/**
Expand Down Expand Up @@ -370,3 +371,6 @@ private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $pa
);
}
}

interface_exists(ClassMetadataInterface::class);
interface_exists(ReflectionService::class);
7 changes: 5 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\Annotations\AbstractIndex;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver;
use ReflectionClass;
use ReflectionMethod;
use const E_USER_DEPRECATED;
Expand All @@ -20,6 +20,7 @@
use function assert;
use function constant;
use function get_class;
use function interface_exists;
use function is_array;
use function trigger_error;

Expand All @@ -44,7 +45,7 @@ public function isTransient($className)
/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Mapping\ClassMetadata $class) : void
public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $class) : void
{
assert($class instanceof ClassMetadata);
$reflClass = $class->getReflectionClass();
Expand Down Expand Up @@ -285,3 +286,5 @@ public static function create($paths = [], ?Reader $reader = null) : AnnotationD
return new self($reader, $paths);
}
}

interface_exists(\Doctrine\Persistence\Mapping\ClassMetadata::class);
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ODM\MongoDB\Mapping\Driver;

use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
use Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator;

/**
* XmlDriver that additionally looks for mapping information in a global file.
Expand Down
Loading

0 comments on commit 697e550

Please sign in to comment.