2

我正在尝试创建 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”。

我使用下一个文件结构进行映射:

参数.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

参数.php

<?php
 namespace Acme\StoreBundle\Document;

class Params 
{
    protected $param2; 

    protected $param4;

    protected $param3;

    protected $subparam31;  

    protected $subparam32; 

    protected $subparam33;
}
?>

我哪里错了?如何获取和设置子参数的值?

为了访问 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
    ); 

我希望我足够清楚。提前致谢。

4

2 回答 2

2

我找到了解决方案!除了 yml 中的映射外,还应该在 php 类中定义适当的注解。

以下是必要文件的内容:

参数.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      

参数.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)
    );
}

}

参数3.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()
    );
}

}

这个问题帮助了我。

于 2013-09-29T10:37:14.153 回答
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
于 2013-09-26T04:02:19.227 回答