假设我们有以下文件与它们之间的关系:
/**
* @ODM\Document(collection="foo")
*/
class Foo
{
/** @ODM\Id(name="_id", strategy="NONE", type="string") */
public $id;
/** @ODM\Field(name="name", type="string") */
public $name;
/**
* @ODM\ReferenceOne(targetDocument="Bar")
*/
public $bar;
}
/**
* @ODM\Document(collection="bar")
*/
class Bar
{
/** @ODM\Id(name="_id", strategy="NONE", type="string") */
public $id;
/** @ODM\Field(name="name", type="string") */
public $name;
}以及以下代码:
$foo = new Foo();
$foo->id = uniqid();
$foo->name = 'thefoo';
$bar = new Bar();
$bar->id = uniqid();
$bar->name = 'thebar';
$foo->bar = $bar;
$dm->persist($bar);
$dm->persist($foo);
$dm->flush();在这种情况下,与"Bar“的关系将以dbRefs格式以mongo格式存储。
{
"_id" : "58ac22815e49d",
"name" : "thefoo",
"bar" : {
"$ref" : "bar",
"$id" : "58ac22815e59a",
"$db" : "mydb"
}
}然而,我想要做的是以这种格式“手动”存储关系:
{
"_id" : "58ac22815e49d",
"name" : "thefoo",
"bar_id" : "58ac22815e59a"
}在Doctrine中是否有一种方法可以映射我的文档以以这样的格式存储关系?
编辑:多亏了马拉兹姆的回答,我通过以下映射实现了预期的结果。
/**
* @ODM\Document(collection="foo")
*/
class Foo
{
/** @ODM\Id(name="_id", strategy="NONE", type="string") */
public $id;
/**
* @ODM\ReferenceOne(name="bar_id", targetDocument="Bar", storeAs="id")
*/
public $bar;
/** @ODM\Field(name="name", type="string") */
public $name;
}发布于 2017-02-21 16:36:43
要使ODM只存储引用文档的id,需要将storeAs="id"添加到参考映射中,有关更多信息,请参见文档中的存储引用章节。
https://stackoverflow.com/questions/42366150
复制相似问题