在我的项目中,我需要在orm和odm之间共享对象。我有一个实体“变体”和一个文档“跟踪”。一个变体可以保存许多跟踪事件。我试着用symfony 2.3的stofdoctrineextensionbundle通过条令扩展引用来解决这个问题,但是无论我做什么都行不通。也许你们中有人有主意。
跟踪文档:(注意$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;
    }

变体实体(注意$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;
}

最佳答案

解决循环引用问题(除非可以使用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]);
    }
}

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

关于mongodb - 在doctrine2 odm和symfony2中的orm之间使用doctrine扩展共享对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17296659/

10-09 20:24