为关联数组选择哪种映射类型?学说ODM

发布于 2024-10-02 17:39:51 字数 615 浏览 0 评论 0原文

我有一个关于 Doctrine ODM 的简单问题(顺便说一下,真的很棒!)。

假设您有一个如下文档:

/**
 * @Document
 */
class Test
{
    /** @Id */
    public $id;
    /** @WHICHTYPE */
    public $field = array();
}

现在我想存储一个关联数组,例如

array("test" => "test1", "anothertest" => "test2", ......);

在该类的 $field 属性中。

我知道,对于 MongoDB 来说没问题,但在 Doctrine 中,当我使用 @Collection 或简单的 @Field 时,仅存储值(例如,在映射驱动程序中使用 array_values 进行集合)。所以存储的值看起来像

array("test1", "test2", ....)

有谁知道我应该使用哪种 Doctrine-ODM 映射类型来保留数据库中的键值对?

提前谢谢你,

安迪(来自德国的问候)

I have a simple question about the (by the way really great!) Doctrine ODM.

Assume you have a document like:

/**
 * @Document
 */
class Test
{
    /** @Id */
    public $id;
    /** @WHICHTYPE */
    public $field = array();
}

Now i want to store an associative array like

array("test" => "test1", "anothertest" => "test2", ......);

In the $field property of that class.

No problem for MongoDB, I know, but in Doctrine when I use for example @Collection or simply @Field, only the values are stored (array_values is being used in the mapping driver for collection for example). So the stored value looks like

array("test1", "test2", ....)

Does anyone know which Doctrine-ODM mapping type I should use in order to preserve the key-value pairs in the database?

Thank you in advance,

Andi (greetz from germany)

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

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

发布评论

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

评论(5

静谧幽蓝 2024-10-09 17:39:51

对于ODM 2.0之前的版本,@Hash将提供必要的数据类型。然而,在 ODM 2.0 之后,@Hash 字段将被删除。为了使用它,我们必须使用散列类型的@field。
如需进一步参考[点击此处][1]

For versions before ODM 2.0 @Hash will provide the necessary data type. However after ODM 2.0 @Hash field is being removed. In order to use it we have to use @field with type hash.
For further reference [click here][1]

故事还在继续 2024-10-09 17:39:51

我认为您正在寻找 hash 数据类型。你不是吗?

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @Document
 */
class Test
{
    /** @Id */
    public $id;

    /**
     * @MongoDB\Field(type="hash")
    */
    public $field;
}

I think you're looking for hash data type. Aren't you?

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @Document
 */
class Test
{
    /** @Id */
    public $id;

    /**
     * @MongoDB\Field(type="hash")
    */
    public $field;
}
念﹏祤嫣 2024-10-09 17:39:51

最好的答案是使用 哈希类型。但如果由于某种原因你不想使用 hash 类型,你可以使用 Doctrine ODM 提供的 EmbeddedDocument 功能,如文档所述:

如果您使用哈希类型,则关联数组中的值
直接传递到 MongoDB,无需准备。仅格式
应该使用适合Mongo驱动程序的。如果你的哈希包含
不合适的值您应该使用嵌入的
文档或使用 MongoDB 驱动程序提供的格式(例如
\MongoDate 而不是 \DateTime)。

因此,您需要在 AppBundle\Document\EmbeddedExample.php 中创建 EmbeddedDocument EmbeddedExample

<?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\EmbeddedDocument()
 */
class EmbeddedExample
{
    /**
     * @MongoDB\Field(type="int")
     */
    protected $some_name;

    // ...
    // getter and setter
}

然后,您可以在 Test 中使用 EmbeddedExample 文档。因此 Test.php 文件将与此类似:

<?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
* @MongoDB\Document(repositoryClass="AppBundle\Repository\TestRepository")
*/
class Test
{

    /** @MongoDB\EmbedOne(targetDocument="EmbeddedExample") */
    private $field;

    // ...
}

The best answer is using hash type. But if for some reason you wantn't use hash type, you can use EmbeddedDocument feature provided by Doctrine ODM like the documentation says:

If you are using the hash type, values within the associative array
are passed to MongoDB directly, without being prepared. Only formats
suitable for the Mongo driver should be used. If your hash contains
values which are not suitable you should either use an embedded
document or use formats provided by the MongoDB driver (e.g.
\MongoDate instead of \DateTime).

So, you need to create EmbeddedDocument EmbeddedExample in AppBundle\Document\EmbeddedExample.php:

<?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\EmbeddedDocument()
 */
class EmbeddedExample
{
    /**
     * @MongoDB\Field(type="int")
     */
    protected $some_name;

    // ...
    // getter and setter
}

Then, you can use EmbeddedExample in your Test document. So the Test.php file will be similar to this:

<?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
* @MongoDB\Document(repositoryClass="AppBundle\Repository\TestRepository")
*/
class Test
{

    /** @MongoDB\EmbedOne(targetDocument="EmbeddedExample") */
    private $field;

    // ...
}
入画浅相思 2024-10-09 17:39:51

@Array 应该可以工作。 ORM 中至少存在等效项 (@Column(type="array"))

@Array should work. At least an equivalent exists in the ORM (@Column(type="array"))

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