开发者

Doctrine 2 ORM and MongoDB ODM mapping on the same class

Is it possible to map the same class to both ORM and ODM using annotations?

We are deciding what to use so we want to do some performance measurment and we also want to be able to switch the persistance mappers easily. I have already done the manager unification, now i would like to unify the classes. Now i have a copy of each class in separate namespaces for Entities and Documents which i 开发者_StackOverflow社区find kind of redundant.

I read this article http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/cookbook/mapping-classes-to-orm-and-odm.html, but in the end i guess they use two different classes, each in their own namespace.

Has anybody tried this?


I've never tried but it's totally possible to put both ODM and ORM mapping on the exact same class.

The problem will maybe rely on synchronization of data between these two persistence backends and the Entity API. For example, if you have a ManyToOne association, ODM will have a different internal in memory reference than ORM. So it's possible that it will override objects you were working with.


Didn't try this before but if i could suggest something is to have different mapping in xml/yml for you entity/document class?


Yes you can. I have done it using symfony and annotations, so I guess you could manage just as well using whatever environment you are using.

First, I added both annotations on the entity:

<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * User
 *
 * @ORM\Entity
 * @ODM\Document
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ODM\Field()
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=200, nullable=false)
     * @ODM\Field()
     */
    private $email;
}

Under symfony the default directory for ORM is the Entity directory, for ODM the default directory is Document. So if you have an entity that has to be a document at the same time, you have to configure either of the two mappings manually.

doctrine_mongodb:
    document_managers:
        default:
            mappings:
                # Default mapping for the bundle (loads Document/)
                DemoBundle: ~
                # Extra mapping to load document mappings under Entity/
                DualMappingHack:
                    type: annotation
                    dir: %kernel.root_dir%/../src/Acme/DemoBundle/Entity
                    prefix: EntityPrefix
                    is_bundle: false
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜