地理空间索引"没有找到字段"与YAML

时间:2012-06-16 17:29:59

标签: mongodb doctrine-orm yaml geospatial embedded-database

我尝试使用Doctrine2的OD与YAML格式和MongoDB映射嵌入式文档以进行地理空间索引时遇到问题: 我会尝试在“Place”文档中嵌入一个“Coordinate”文档,以便能够执行一些地理空间索引查询,但是会遇到映射问题。

正如官方文件所示: 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软件包的Res​​ources \ 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()
        );
    }
}

提前多多谢谢你, 波比。

1 个答案:

答案 0 :(得分:-1)

您应该使用注释而不是YML来映射文档。

它非常容易。

祝你好运! ;)