这是一个文档示例:
"_id" : ObjectId("52ae09f8211b6f57548b4568"),
"active" : 1,
"author" : "582127753",
"contact" : "ntrms-4148701995@domain.com",
"coordinates" : {
"lat" : 35.691048,
"lng" : 139.701065
},
"date" : ISODate("2013-10-24T18:41:42Z")
现在当我运行查询来获取此文档时,我得到了这样的答案
array(2) {
["52ae09f8211b6f57548b4568"]=>
object(ng\NearBundle\Document\Markers)#213 (14) {
["id":protected]=>
string(24) "52ae09f8211b6f57548b4568"
["coordinates":protected]=>
object(ng\NearBundle\Document\Coordinates)#216 (2) {
["lat":protected]=>
NULL
["lng":protected]=>
NULL
}
坐标文件:
<?php
namespace ng\NearBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/** @MongoDB\EmbeddedDocument */
class Coordinates
{
/** @MongoDB\Float */
protected $lat;
/** @MongoDB\Float */
protected $lng;
public function __construct($lat = 0.0, $lng = 0.0)
{
$this->setLat($lat);
$this->setLng($lng);
}
/**
* Set lat
*
* @param float $lat
* @return \Coordinates
*/
public function setLat($lat)
{
$this->lat = $lat;
return $this;
}
/**
* Get lat
*
* @return float $lat
*/
public function getLat()
{
return $this->lat;
}
/**
* Set lng
*
* @param float $lng
* @return \Coordinates
*/
public function setLng($lng)
{
$this->lng = $lng;
return $this;
}
/**
* Get lng
*
* @return float $lng
*/
public function getLng()
{
return $this->lng;
}
}
并在引用它的初始文档中:
/** @MongoDB\EmbedOne(targetDocument="Coordinates") */
protected $coordinates;
谁能告诉我什么是错的?
答案 0 :(得分:0)
我无法通过以下测试用例重现您的问题:
namespace Doctrine\ODM\MongoDB\Tests;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
class SO21091204Test extends BaseTest
{
public function test1()
{
$coordinates = new TestCoordinates();
$coordinates->lat = 1.2;
$coordinates->lng = 3.4;
$document = new TestDocument();
$document->coordinates = $coordinates;
$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();
$document = $this->dm->find(get_class($document), $document->id);
$this->assertSame(1.2, $document->coordinates->lat);
$this->assertSame(3.4, $document->coordinates->lng);
}
}
/** @ODM\Document */
class TestDocument
{
/** @ODM\Id */
public $id;
/** @ODM\EmbedOne(targetDocument="TestCoordinates") */
public $coordinates;
}
/** @ODM\EmbeddedDocument */
class TestCoordinates
{
/** @ODM\Float */
public $lat;
/** @ODM\Float */
public $lng;
}
由于您的文档示例可能来自MongoDB shell(它看起来像JSON),您可能希望通过ODM发出非水合查询以查看PHP中的原始值。这至少会向您显示ODM正在接收的数据:
$rs = $dm->createQueryBuilder('ng\NearBundle\Document\Markers')
->field('id')->equals('52ae09f8211b6f57548b4568')
->hydrate(false)
->getQuery()
->getSingleResult();
var_dump($rs);