Doctrine 2 ORM 和 MongoDB ODM 映射在同一个类上

发布于 2024-12-09 03:49:59 字数 442 浏览 3 评论 0原文

是否可以使用注释将同一个类映射到 ORM 和 ODM?

我们正在决定使用什么,因此我们想要进行一些性能测量,并且我们还希望能够轻松切换持久性映射器。我已经完成了经理统一,现在我想统一班级。现在,我在实体和文档的单独命名空间中拥有每个类的副本,我发现这有点多余。

我读了这篇文章 http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/cookbook/mapping-classes-to-orm-and-odm.html,但最后我猜他们使用了两个不同的类,每个类都在自己的命名空间中。

有人尝试过这个吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深已缘浅 2024-12-16 03:49:59

我从未尝试过,但完全有可能将 ODM 和 ORM 映射放在完全相同的类上。

该问题可能依赖于这两个持久性后端之间的数据同步
和实体 API。例如,如果您有一个 ManyToOne 关联,ODM 将具有与 ORM 不同的内部内存引用。因此它可能会覆盖您正在使用的对象。

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.

蓝眼泪 2024-12-16 03:49:59

之前没有尝试过,但是我是否可以建议在 xml/yml 中为您的实体/文档类提供不同的映射?

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

素手挽清风 2024-12-16 03:49:59

是的,你可以。我已经使用 symfony 和注释完成了它,所以我想您可以使用您正在使用的任何环境进行管理。

首先,我在实体上添加了两个注释:

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

在 symfony 下,ORM 的默认目录是 Entity 目录,对于 ODM 的默认目录是 Document。因此,如果您有一个实体必须同时是文档,则必须手动配置这两个映射中的一个。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文