Question

in my project i need to share objects between orm and odm. I have an entity "Variation" and a document "Tracking". One variation can hold many trackingevents. I tried to solve it through doctrine extension references using the stofdoctrineextensionbundle for symfony 2.3 but whatever i do it wont work. Maybe someone of you have an idea.

Tracking Document: ( watch for $variation )

namespace Anchorbrands\Bundle\LandingpageBundle\Document;

use Anchorbrands\Bundle\LandingpageBundle\AnchorbrandsLandingpageBundle;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;


/**
 * @ODM\Document(collection="landingpage_tracking")
 */
class Tracking {

    /**
     * @Gedmo\ReferenceOne(type="entity", class="Anchorbrands\Bundle\LandingpageBundle\Entity\Variation", inversedBy="trackingEvents", identifier="variationId")
     */
    protected $variation;

    protected $variationId;

    public function setVariationId($variationId)
    {
        $this->variationId = $variationId;
    }

    public function getVariationId()
    {
        return $this->variationId;
    }

Variation Entity ( watch out for $trackingEvents )

/**
 * Variation
 *
 * @ORM\Table(name="landingpage_variation")
 * @ORM\Entity
 */
class Variation
{

/**
 * @Gedmo\ReferenceMany(type="document", class="Anchorbrands\Bundle\LandingpageBundle\Document\Tracking", mappedBy="variation")
 */
protected $trackingEvents;


public function getTrackingEvents()
{
    return $this->trackingEvents;
}

public function setTrackingEvents(Collection $trackingEvents)
{
    $this->trackingEvents = $trackingEvents;
}

No correct solution

OTHER TIPS

Workaround to the circular referencing problem (unless you can solve it using DI) @see stof's bundle

<?php

namespace ACME\CoreBundle\Listener;

use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class ReferencesListener
 *
 * @package ACME\CoreBundle\Listener
 */
class ReferencesListener extends \Gedmo\References\ReferencesListener
{
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;
    /**
     * @var array
     */
    protected $managers
        = [
            'document' => 'doctrine.odm.mongodb.document_manager',
            'entity'   => 'doctrine.orm.default_entity_manager'
        ];

    /**
     * @param ContainerInterface $container
     * @param array              $managers
     */
    public function __construct(ContainerInterface $container, array $managers = array())
    {
        $this->container = $container;
        parent::__construct($managers);
    }

    /**
     * @param $type
     *
     * @return object
     */
    public function getManager($type)
    {
        return $this->container->get($this->managers[$type]);
    }
} 

Listener defination

acme.listener.reference:
       class: ACME\CoreBundle\Listener\ReferencesListener
       arguments: ["@service_container"]
       tags:
           - { name: doctrine.event_subscriber, connection: default }

When using doctrine extension's references you need to make sure you have registered the subscriber with doctrine. Otherwise the references won't work.

In symfony2 you can register doctrine listeners/subscribers using container tags:

config.yml

services:
    gedmo_reference_listener:          # choose whatever name you like
        class: Gedmo\References\ReferencesListener
        arguments: 
            - { entity: @doctrine.orm.entity_manager, document: @doctrine.odm.mongodb.document_manager }
        tags:
            - { name: doctrine.event_subscriber, connection: default }

This is what StofDoctrineExtensionsBundle usually does in the bundle's compilerpass. It just simplifies the process of registering the services using a bit of configuration.

But as you can see here ... @stof didn't yet add the reference listener to the configuration options.

There is already an open pull request but @stof doesn't want to add it until the implementation has been refactored. Meanwhile use my solution above :-)

Maybe try from :

services:
    # reference behavior doctrine < mongodb
    gedmo.listener.reference:
        class: Gedmo\References\ReferencesListener
        tags:
            - { name: doctrine_mongodb.odm.event_subscriber }
        calls:
            - [ setAnnotationReader, [ "@annotation_reader" ] ]
            - [ registerManager, [ 'entity', "@doctrine.orm.default_entity_manager" ] ]

    # reference behavior doctrine > mongodb
    gedmo.listener.reference:
        class: Gedmo\References\ReferencesListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }

        calls:
            - [ setAnnotationReader, [ "@annotation_reader" ] ]
            - [ registerManager, [ 'document', "@doctrine_mongodb.odm.document_manager" ] ]

They both work for me, but alone...

If you put them both like this only the second is taken

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top