在doctrine2 odm和symfony2中的orm之间使用doctrine扩展共享对象

时间:2013-06-25 11:55:05

标签: mongodb symfony doctrine-orm doctrine-extensions stofdoctrineextensions

在我的项目中,我需要在orm和odm之间共享对象。我有一个实体“Variation”和一个文档“Tracking”。一种变化可以容纳许多跟踪事件。我尝试使用stofdoctrineextensionbundle为symfony 2.3通过教条扩展引用来解决它,但无论我做什么都不会工作。也许有人有你的想法。

追踪文件:(注意$变异)

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

3 个答案:

答案 0 :(得分:1)

解决循环引用问题(除非您可以使用DI解决)@see stof的捆绑

<?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 }

答案 1 :(得分:0)

使用doctrine扩展名的引用时,您需要确保已注册the subscriber学说。否则引用将无效。

在symfony2中,你可以register使用容器标签来教导听众/订阅者:

<强> 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 }

这就是StofDoctrineExtensionsBundle通常在包compilerpass中执行的操作。它只是简化了使用一些配置注册服务的过程。

但是你可以看到here ... @stof还没有将参考监听器添加到配置选项中。

已经有一个开放的pull request,但@stof不希望在实现重构之前添加它。同时使用我上面的解决方案: - )

答案 2 :(得分:0)

也许可以试试:

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" ] ]

他们都为我工作,但一个人......

如果你把它们都放在这两个只有第二个