我正在尝试创建symfony2应用程序,它将使用以下结构访问mongoDB集合:
{
"_id": ObjectId("5239c1c0359bf908058a5071"),
"param2": "test",
"param3": {
"subparam31": 0,
"subparam32": 0,
"subparam33": 0
},
"param4": 1
}
在symfony 2中,我创建了一个.yml和php类。我正确映射只有“_id”,“param2”和“param4”,而不是“param3”的“subparam31”,“subparam32”和“subparam33”。
我使用下一个文件结构进行映射:
Params.mongodb.yml:
Acme\StoreBundle\Document\Params:
db: test
type: document
fields:
id:
id: true
param2:
type: string
param3:
type: mixed
subparam31:
type: float
subparam32:
type: float
subparam33:
type: float
param4:
type: float
Params.php
<?php
namespace Acme\StoreBundle\Document;
class Params
{
protected $param2;
protected $param4;
protected $param3;
protected $subparam31;
protected $subparam32;
protected $subparam33;
}
?>
我哪里错了?如何获取和设置subparams的值?
为了访问param2,param4和id,我在控制器中使用以下代码:
$parameter = $this->get('doctrine_mongodb')
->getRepository('AcmeStoreBundle:Params')
->find($id);
$parameter2 = $parameter->getParam2();
$parameter4 = $parameter->getParam4();
if (!$format) {
throw $this->createNotFoundException('Not found parameter with id -> '.$id);
}
return array(
"parameter2" => $parameter2,
"parameter4" => $parameter4
);
我希望我足够清楚。 提前谢谢。
答案 0 :(得分:2)
我找到了解决方案!除了在yml中进行映射之外,还应该定义php类中的适当注释。
以下是必要文件的内容:
Params.mongodb.yml
Acme\StoreBundle\Document\Params:
db: test
type: document
embedOne:
param3:
targetDocument: Param3
fields:
id:
id: true
param2:
type: string
param4:
type: float
Param3.mongodb.yml
Acme\StoreBundle\Document\Param3:
db: test
type: embeddedDocument
fields:
subparam31:
type: float
subparam32:
type: float
subparam33:
type: float
Params.php
<?php
namespace Acme\StoreBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\Document
*/
class Params
{
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\Id
*/
protected $id;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\String
*/
protected $param2;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\Float
*/
protected $param4;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\EmbedOne(targetDocument="Param3")
*/
protected $param3;
public function __construct($subparam31 = NULL, $subparam32 = NULL, $subparam33 = NULL)
{
$param3 = new Param3($subparam31, $subparam32, $subparam33);
$this->setParam3($param3);
}
/**
* Get id
*
* @return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set param2
*
* @param string $param2
* @return self
*/
public function setParam2($param2)
{
$this->param2 = $param2;
return $this;
}
/**
* Get param2
*
* @return string $param2
*/
public function getParam2()
{
return $this->param2;
}
/**
* Set param4
*
* @param float $param4
* @return self
*/
public function setParam4($param4)
{
$this->param4 = $param4;
return $this;
}
/**
* Get param4
*
* @return float $param4
*/
public function getParam4()
{
return $this->param4;
}
/**
* Set param3
*
* @param Acme\StoreBundle\Document\Param3 $param3
* @return self
*/
public function setParam3(\Acme\StoreBundle\Document\Param3 $param3)
{
$this->param3 = $param3;
return $this;
}
/**
* Get param3
*
* @return Acme\StoreBundle\Document\Param3 $param3
*/
public function getParam3($toArray = false)
{
if ($toArray) {
if ($this->param3) {
return $this->param3->toArray();
}
}
return $this->param3;
}
public function toArray()
{
return array(
'param3' => $this->getParam3(true)
);
}
}
Param3.php
<?php
namespace Acme\StoreBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\EmbeddedDocument
*/
class Param3
{
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\Float
*/
protected $subparam31;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\Float
*/
protected $subparam32;
/**
* @Doctrine\ODM\MongoDB\Mapping\Annotations\Float
*/
protected $subparam33;
public function __construct($subparam31 = NULL, $subparam32 = NULL, $subparam33 = NULL)
{
$this->subparam31 = $subparam31;
$this->subparam32 = $subparam32;
$this->subparam33 = $subparam33;
}
/**
* Set subparam31
*
* @param float $subparam31
* @return self
*/
public function setSubparam31($subparam31)
{
$this->subparam31 = $subparam31;
return $this;
}
/**
* Get subparam31
*
* @return float $subparam31
*/
public function getSubparam31()
{
return $this->subparam31;
}
/**
* Set subparam32
*
* @param float $subparam32
* @return self
*/
public function setSubparam32($subparam32)
{
$this->subparam32 = $subparam32;
return $subparam32;
}
/**
* Get subparam32
*
* @return float $subparam32
*/
public function getSubparam32()
{
return $this->subparam32;
}
/**
* Set subparam33
*
* @param float $subparam33
* @return self
*/
public function setSubparam33($subparam33)
{
$this->subparam33 = $subparam33;
return $this;
}
/**
* Get subparam33
*
* @return float $subparam33
*/
public function getSubparam33()
{
return $this->subparam33;
}
public function toArray()
{
return array(
'subparam31' => $this->getSubparam3(),
'subparam32' => $this->getSubparam32(),
'subparam33' => $this->getSubparam33()
);
}
}
这question帮助了我。
答案 1 :(得分:0)
我认为你要找的是EmbeddedDocument。
为param3
(包括subparam31,subparam32和subparam33)定义一个单独的文档,将其设置为params中的targetDocument。所以Params.mongodb.yml
看起来像是:
db: test
type: document
embedOne:
params3:
targetDocument: params3Class
fields:
id:
id: true
param2:
type: string
param4:
type: float