PHPackages                             electricmaxxx/doctrine-orm-odm-adapter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. electricmaxxx/doctrine-orm-odm-adapter

ActiveLibrary

electricmaxxx/doctrine-orm-odm-adapter
======================================

Mapping for using ODM on ORM

141.5k2[14 issues](https://github.com/ElectricMaxxx/DoctrineOrmOdmAdapter/issues)[1 PRs](https://github.com/ElectricMaxxx/DoctrineOrmOdmAdapter/pulls)PHP

Since May 10Pushed 9y ago5 watchersCompare

[ Source](https://github.com/ElectricMaxxx/DoctrineOrmOdmAdapter)[ Packagist](https://packagist.org/packages/electricmaxxx/doctrine-orm-odm-adapter)[ RSS](/packages/electricmaxxx-doctrine-orm-odm-adapter/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

What the hell is he doing here?
===============================

[](#what-the-hell-is-he-doing-here)

[![Build Status](https://camo.githubusercontent.com/d6de368b65d41b61e813240637f374e58277133cfa2d9a2de13de749b8eb824e/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f456c6563747269634d617878782f446f637472696e654f726d4f646d416461707465722e706e67)](http://travis-ci.org/ElectricMaxxx/DoctrineOrmOdmAdapter)

I realized in a client project that there are use cases to reference documents (i.e. from phpcr-odm) on an entity (ORM). The reference is done by the document's uuid. So i started with implementing an listener, that handles the loading, persisting and removing the document for the entity depending on the persisted uuid.

As this handling can be more and more complex i saw the need of an mapping. So this library was born. Atm i try to map the following fields/properties

- uuid - the field for the uuid on the entity
- document - the field for the document on the entity
- common-field - fields that should be synced on both document and entity i.e. the title, to not load the complete document when just displaying

Current state:

- ClassMetadata + Test
- ClassMetadataFactory + Test
- XmlDriver + Test
- AnnotationDriver + Test
- YmlDriver + Test
- DocumentAdapter + Test
- Kind of UnitOfWork for handling hard work
- implement the lifecycle events
- Bundle to use that library ()

Usage
=====

[](#usage)

Configuration
-------------

[](#configuration)

To create a a new instance of an `ObjectAdapterManager` you will need to setup some simple configurations. The class will work without that, but you won't have any managers to persist your referenced objects:

```
     use Doctrine\Common\Annotations\AnnotationReader;
     use Doctrine\Common\Cache\ArrayCache;
     use Doctrine\Common\EventManager;
     use Doctrine\ODM\PHPCR\DocumentManager;
     use Doctrine\ORM\EntityManager;
     use Doctrine\ORM\ODMAdapter\Mapping\Driver\AnnotationDriver;
     use Doctrine\ORM\ODMAdapter\ObjectAdapterManager;
     use Doctrine\Common\EventManager;

     // caching and annotation read
     $cache = new ArrayCache();
     $reader = new AnnotationReader($cache);

     // AnnotationDriver as example, Yaml and Xml available too
     $annotationDriver = new AnnotationDriver($reader);
     $annotationDriver->addPaths(array(__DIR__ . "/Models"));

     // configuration for the manager
     $configuration = new Configuration();
     $configuration->setManagers(
        array(
            'reference-phpcr' => array(
                'default'  => $documentManager,
            ),
            'reference-dbal-orm' => array(
                'defaulKt'  => $entityManager,
            ),
        )
     );
     $configuration->setClassMetadataFactoryName('Doctrine\ORM\ODMAdapter\Mapping\ClassMetadataFactory');
     $configuration->setMetadataDriverImpl($annotationDriver);

     // create the ObjectAdapterManager with the configuration and optional event manager
     $objectAdapterManager = ObjectAdapterManager::create($configuration, new EventManager());

     // makes it possible to use the library's own event subscribers for the managers lifecycle events
     $objectAdapterManager->addListenersToEventManagers();
```

That configuration looks very similar to the common doctrine one and that is the purpose. There are two more mapping driver available. One for Yaml and one for Xml. There is one difference: You have to setup the managers you wanna use as for the reference of an object and for the referencing object. The managers are sorted by the type of mapping (reference-phpcr and reference-dbal-orm for phpcr-odm and orm) and the manager name.

The Main purpose of this library is that such example would work without doing some work on the referenced object:

```
     $entity = new ReferencingEntity();

     $document = new ReferencedDocument();
     $document->name = 'document-name';
     $document->parentDocument =  // create some parent document
     $entity->document = $this->referencedObject;

     $entityManager->persist($entity);
     $entityManager->flush();
     $entityManager->clear();

     $document = $documentManager->find(null, '/some-path/document-name');

     // or even better
     $entity = $entityManager->find('Entity', $entity->id);
     $document = $entity->document;
```

In general this documentation speaks about an object that references the referenced object, but the examples tries to explains that with an entity that references documents (or inverted). But how should that example work? We can do some custom event hooking for every use case or just persist the referenced objects manually.

Mapping
-------

[](#mapping)

But the mapping of this library should help in such a situation. You can do it in Xml:

```

```

Yaml

```

    Doctrine\Tests\ORM\ODMAdapter\Mapping\Driver\Model\ReferenceMappingObject:
      referencedField:
          type: reference-phpcr
          referenced-by: uuid
          inversed-by: uuid
          target-object: Document

```

or Annotations

```
    use Doctrine\ORM\ODMAdapter\Mapping\Annotations as ODMAdapter;

     /**
      * @ODMAdapter\ObjectAdapter
      */
     class Entity
     {
         public $uuid;

         /**
          * @ODMAdapter\ReferencePhpcr(
          *  referencedBy="uuid",
          *  inversedBy="uuid",
          *  targetObject="Document",
          *  manager="default"
          * )
          */
         public $document;
     }
```

The reference mapping is wrapped by an item (xml-node, annotation,...) which describes the type of the reference, means the doctrine the referenced object lives in. At the moment there are two different types available:

- reference-phpcr -&gt; to create a reference on a document persisted with phpcr-odm
- reference-dbal-orm -&gt; to create a reference on an entity persisted with doctrines orm

The body of those types got same attributes for all:

- referenced-by -&gt; the identifier to reference the object (`$manager->find(null, $id))` should work)
- inversed-by -&gt; the field to store the value of the referenced objects field (referenced-by) target-object -&gt; the FQCN of the referenced object
- name -&gt; field where to find the referenced objct
- manager -&gt; name of the manger (set in the configuration), default set to default
- common-field -&gt; fields to keep in sync between both objects (see own description)

If the managers you have set to the configuration provide own event managers, that will be enough. The library is able to hook own EventSubscriber on the lifecycle events fired by the UnitOfWork of the object that holds the reference. Otherwise you will need to build your own EventSystem and call corresponding methods on the `ObjectAdapterManager`. Just take the library's own as example:

```
