Geospatial Indexing “No mapping found for field” with YAML
-
14-06-2021 - |
Pregunta
I have a problem trying to map an embedded document for geospatial indexing using Doctrine2's OD with YAML format, and MongoDB : I would try to embed a "Coordinate" Document in the "Place" document to be able to execute some geospatial indexed queries, but got a mapping problem.
As seen on the official documentation : http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/indexes.html#geospatial-indexing
But I work with YAML mapping format... And got this problem :
"No mapping found for field '/Applications/MAMP/htdocs/intramuros-web/src/IntraMuros/CoreBundle/Resources/config/doctrine/Coordinates.mongodb.yml' in class 'IntraMuros\CoreBundle\Document\Coordinates'."
Here's my YAML file : Place.mongodb.yml, correctly placed in Resources\config directory of my Symfony2's bundle.
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
And Here's the PHP Document class I would to use.
<?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()
);
}
}
Thank you a lot in advance, Bobby.
Solución
You should use annotations instead of YML for mapping your documents.
It will work very easily.
Good luck ! ;)