我在尝试使用 Doctrine2 的 OD 和 YAML 格式和 MongoDB 映射嵌入文档以进行地理空间索引时遇到问题:我会尝试在“位置”文档中嵌入“坐标”文档,以便能够执行一些地理空间索引查询,但是有一个映射问题。
如官方文档所示: http: //docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/indexes.html#geospatial-indexing
但我使用 YAML 映射格式......并遇到了这个问题:
“在类'IntraMuros\CoreBundle\Document\Coordinates'中找不到字段'/Applications/MAMP/htdocs/intramuros-web/src/IntraMuros/CoreBundle/Resources/config/doctrine/Coordinates.mongodb.yml'的映射。”
这是我的 YAML 文件:Place.mongodb.yml,正确放置在我的 Symfony2 包的 Resources\config 目录中。
IntraMuros\CoreBundle\Document\Place:
type: document
db: intramuros
collection: places
repositoryClass: IntraMuros\CoreBundle\Repository\PlaceRepository
fields:
id:
type: id
id: true
name:
type: string
address:
type: string
coordinates:
embedded: true
type: one
targetDocument: IntraMuros\CoreBundle\Document\Coordinates
cascade: all
strategy: set
indexes:
coordinates:
keys:
coordinates: 2d
IntraMuros\CoreBundle\Document\Coordinates:
type: EmbeddedDocument
db: intramuros
fields:
latitude:
type: float
longitude:
type: float
这是我要使用的 PHP Document 类。
<?php
namespace IntraMuros\CoreBundle\Document;
/**
* @Document(requireIndexes=true)
* @Indexes({
* @Index(keys={"coordinates"="2d"})
* })
*/
class Place
{
/**
* @Id
*/
protected $id;
/**
* @String
*/
protected $name;
/**
* @String
*/
protected $address;
/**
* @EmbedOne(targetDocument="IntraMuros\CoreBundle\Document\Coordinates")
*/
protected $coordinates;
public function __construct($latitude = NULL, $longitude = NULL)
{
$coordinates = new Coordinates($latitude, $longitude);
$this->setCoordinates($coordinates);
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setAddress($address)
{
$this->address = $address;
}
public function getAddress()
{
return $this->address;
}
public function setCoordinates($coordinates)
{
$this->coordinates = $coordinates;
}
public function getCoordinates($toArray = false)
{
if ($toArray) {
if ($this->coordinates) {
return $this->coordinates->toArray();
}
}
return $this->coordinates;
}
public function toArray()
{
return array(
'id' => $this->getId(),
'name' => $this->getName(),
'address' => $this->getAddress(),
'coordinates' => $this->getCoordinates(true)
);
}
}
/**
* @EmbeddedDocument
*/
class Coordinates
{
/**
* @Float
*/
protected $latitude;
/**
* @Float
*/
protected $longitude;
public function __construct($latitude = NULL, $longitude = NULL)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
}
public function setLatitude($latitude)
{
$this->latitude = $latitude;
}
public function getLatitude()
{
return $this->latitude;
}
public function setLongitude($longitude)
{
$this->longitude = $longitude;
}
public function getLongitude()
{
return $this->longitude;
}
public function toArray()
{
return array(
'latitude' => $this->getLatitude(),
'longitude' => $this->getLongitude()
);
}
}
非常感谢你,鲍比。