Php 如何在doctrine mongodb中执行嵌套引用查询
这描述了我当前的模式:Php 如何在doctrine mongodb中执行嵌套引用查询,php,mongodb,symfony,doctrine-orm,doctrine,Php,Mongodb,Symfony,Doctrine Orm,Doctrine,这描述了我当前的模式: /** * @MongoDB\Document(repositoryClass="St\AppBundle\Repository\TaxiStateRepository", requireIndexes=true) * @MongoDB\Index(keys={"location"="2d"}) */ class TaxiState { /** * @MongoDB\ReferenceOne(targetDocument="Taxi", sim
/**
* @MongoDB\Document(repositoryClass="St\AppBundle\Repository\TaxiStateRepository", requireIndexes=true)
* @MongoDB\Index(keys={"location"="2d"})
*/
class TaxiState
{
/**
* @MongoDB\ReferenceOne(targetDocument="Taxi", simple=true, inversedBy="taxiState")
* @MongoDB\Index
*/
protected $taxi;
..
}
/**
* @MongoDB\Document(repositoryClass="St\AppBundle\Repository\TaxiRepository", requireIndexes=true)
*/
class Taxi
{
/**
* @MongoDB\ReferenceOne(targetDocument="Driver", simple=true)
* @MongoDB\Index
*/
protected $driver;
..
}
/**
* @MongoDB\Document(repositoryClass="St\AppBundle\Repository\DriverRepository", requireIndexes=true)
*/
class Driver
{
/**
* @MongoDB\EmbedOne(targetDocument="DriverAccount")
* @MongoDB\Index
*/
protected $driverAccount;
..
}
/** @MongoDB\EmbeddedDocument */
class DriverAccount
{
/**
* @MongoDB\String
* @Assert\NotBlank()
* @Assert\Choice(choices = {"enabled","disabled"}, message="please chose a valid status"); * @MongoDB\Index
*/
protected $status;
我基本上想运行一个查询,过滤掉禁用的驱动程序帐户。。大概是这样的:
return $this->createQueryBuilder()
->field('taxi.driver.driverAccount.status')->equals("enabled")
->getQuery()
->getSingleResult();
它抱怨没有索引出租车司机等。。我花了一整天的时间研究条令中的文件,但这些例子太少了。。帮忙
供参考。。在我介绍这句疯狂的话之前,这个问题就已经奏效了:
return $this->createQueryBuilder()
->field('status')->equals('available')
->field('taxi')->notIn($taxiObj)
->field('location')->near((float)$location->getLongitude(), (float)$location->getLatitude())
->distanceMultiplier(self::EARTH_RADIUS_KM)
->maxDistance($radius/111.12)
->getQuery()
->execute();
以防万一,你想知道我是如何“解决”这个问题的(这是一个非常粗糙的答案……但哦,你做了你应该做的事吗?),这就是我得到的:
/**
* Find near enabled taxi without rejected request taxi
*
* @param Document\Location $location
* @param int $radius
* @param array $taxis
* @return Document\TaxiState
*/
public function findEnabledNearTaxiWithoutRejectRequest(Document\Location $location, $radius = self::SEARCH_RADIUS, $taxis = array(), $logger)
{
$taxiObj = array_map(function ($item) {
return new \MongoId($item);
}, $taxis);
//ST-135 change to near, spherical, distanceMultiplier and maxDistance in KM
$allTaxiStates = $this->createQueryBuilder()
->field('status')->equals('available')
->field('taxi')->notIn($taxiObj)
->field('location')->near((float)$location->getLongitude(), (float)$location->getLatitude())
->distanceMultiplier(self::EARTH_RADIUS_KM)
->maxDistance($radius/111.12)
->getQuery()
->execute();
$this->addIdsOfDisabledTaxiStates($taxiObj, $allTaxiStates);
if (count($taxiObj) > 0) {
$logger->info("There are ".count($taxiObj)." taxis excluded while looking for taxis to respond to a requst: ".$this->getMongoIdsStr($taxiObj));
}
return $this->createQueryBuilder()
->field('status')->equals('available')
->field('taxi')->notIn($taxiObj)
->field('location')->near((float)$location->getLongitude(), (float)$location->getLatitude())
->distanceMultiplier(self::EARTH_RADIUS_KM)
->maxDistance($radius/111.12)
->getQuery()
->getSingleResult();
}
/**
* Get the Mongo Ids of disabled taxi States
*
* @param $ids existing array of ids we want to append to (passed by reference)
* @param $taxiStates array of Document\TaxiState
* @return array of MongoIds of disabled taxi states
* @author Abdullah
*/
private function addIdsOfDisabledTaxiStates(&$ids, $taxiStates)
{
foreach ($taxiStates as $taxiState) {
if ($taxiState->getTaxi()->getDriver()->getDriverAccount()->getStatus() != DriverAccountModel::STATUS_ENABLED) {
$mongoId = new \MongoId($taxiState->getTaxi()->getId());
array_push($ids, $mongoId);
}
}
return $ids;
}