geoNear クエリを実行するときに、ドキュメントで mongodb 距離値を取得する際に問題が発生しています。
関連する 3 つのドキュメントがあります: 場所を埋め込む場所と座標を埋め込む場所です。
これが私のモデルの「ライト」バージョンです
会場 :
/**
* Venue
*
* @ODM\Document(repositoryClass="CLabs\VenueBundle\Document\VenueRepository")
*/
class Venue
{
/**
* @var integer
*
* @ODM\Id(strategy="auto")
*/
protected $id;
/**
* @var float
*
* @ODM\Distance
* @ODM\NotSaved
*/
public $distance;
/**
* @var \Location
*
* @ODM\EmbedOne(
* targetDocument="CLabs\LocationBundle\Document\Location"
* )
*/
protected $location;
/**
* Get id
*
* @return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set distance
*
* @param float $distance
* @return self
*/
public function setDistance($distance)
{
$this->distance = $distance;
return $this;
}
/**
* Get distance
*
* @return float $distance
*/
public function getDistance()
{
return $this->distance;
}
/**
* Set location
*
* @param CLabs\LocationBundle\Document\Location $location
* @return self
*/
public function setLocation(\CLabs\LocationBundle\Document\Location $location)
{
$this->location = $location;
return $this;
}
/**
* Get location
*
* @return CLabs\LocationBundle\Document\Location $location
*/
public function getLocation()
{
return $this->location;
}
}
位置 :
/**
* Location
*
* @ODM\EmbeddedDocument
* @ODM\Index(keys={"coordinates"="2d"})
*/
class Location
{
/**
* @var \Coordinates
*
* @ODM\EmbedOne(
* targetDocument="CLabs\LocationBundle\Document\Coordinates"
* )
*/
protected $coordinates;
/**
* Set coordinates
*
* @param CLabs\LocationBundle\Document\Coordinates $coordinates
* @return self
*/
public function setCoordinates(\CLabs\LocationBundle\Document\Coordinates $coordinates)
{
$this->coordinates = $coordinates;
return $this;
}
/**
* Get coordinates
*
* @return CLabs\LocationBundle\Document\Coordinates $coordinates
*/
public function getCoordinates()
{
return $this->coordinates;
}
}
座標:
/**
* Coordinates
* @ODM\EmbeddedDocument
*/
class Coordinates
{
/**
* @var string
*
* @ODM\Float
*/
protected $lng;
/**
* @var string
*
* @ODM\Float
*/
protected $lat;
/**
* Set lng
*
* @param float $lng
* @return self
*/
public function setLng($lng)
{
$this->lng = $lng;
return $this;
}
/**
* Get lng
*
* @return float $lng
*/
public function getLng()
{
return $this->lng;
}
/**
* Set lat
*
* @param float $lat
* @return self
*/
public function setLat($lat)
{
$this->lat = $lat;
return $this;
}
/**
* Get lat
*
* @return float $lat
*/
public function getLat()
{
return $this->lat;
}
}
このモデルから、次のような VenueRepository で geoNear クエリを実行すると:
$kmsMultiplier = 6378.137;
return $this->createQueryBuilder()
->geoNear((float)$lng, (float)$lat)
->maxDistance($maxDistance/$kmsMultiplier)
->spherical(true)
// Convert radians to kilometers
->distanceMultiplier($kmsMultiplier)
->getQuery()
->execute();
'$result->toArray()' (または単純に結果の foreach) を実行してオブジェクト コレクションを抽出します。
しかし、mongodbが距離を適切に計算したとしても、私が行う$venue->getDistance()
と常に が返されます。null
このクエリの結果のサンプルを次に示します。
"elements": [
{
"id": "559f7b728b8ff7c1127b23c8",
"location": {
"coordinates": {
"lng": 5.4190150243677,
"lat": 43.533426029669
}
}
}...
],
"commandResult": {
"results": [
{
"dis": 3.1465013027305,
"obj": {
"_id": {
"$id": "559f7b728b8ff7c1127b23c8"
},
"location": {
"coordinates": {
"lng": 5.4190150243677,
"lat": 43.533426029669
}
}
}
}...
],
"stats": {
"nscanned": 3,
"objectsLoaded": 3,
"avgDistance": 3.8863491981201,
"maxDistance": 4.3498686266835,
"time": 0
},
"ok": 1
}
get/setDistance を Venue ドキュメントから削除し、$distance 宣言を Location ドキュメントに移動しようとしましたが、何も機能しません。nor からの距離を取得できませ$object->getDistance()
ん$object->distance
。
このプロパティ マッピングを自分で処理することもできますが、代わりに Doctrine メソッドを使用する方がはるかに便利です。それが機能する場合は...どこが間違っているのか、それとも自分のコードが原因なのかわかりません。
ここに私のcomposer.jsonのサンプルがあります:
"symfony/symfony": "v2.6.4",
"doctrine/mongodb": "v1.1.8",
"doctrine/mongodb-odm": "v1.0.0-BETA13",
"doctrine/mongodb-odm-bundle": "v3.0.0"
debian VM (Linux wheezy64 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u3 x86_64 GNU/Linux) に mongodb 2.6.11 をインストールしました。
Doctrine ODM の問題だと思いますか? この問題を抱えているのは私だけですか?
どんな助けでも大歓迎