简体   繁体   English

如何在学说mongodb中执行嵌套参考查询

[英]How to perform nested reference query in doctrine mongodb

This describes my current schema: 这描述了我当前的模式:

/**
 * @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;

I basically want to run a query that filters out disabled driver accounts.. something like this: 我基本上想运行一个查询,以过滤掉禁用的驱动程序帐户。

return $this->createQueryBuilder()
    ->field('taxi.driver.driverAccount.status')->equals("enabled")
    ->getQuery()
    ->getSingleResult();

it complains that it doesn't have an index taxi.driver etc.. I spent all day looking at by directional reference documentation in doctrine but the examples are so sparse.. help? 它抱怨说它没有index taxi.driver等。我花了一整天的时间在教义上通过定向参考文档进行研究,但是示例如此稀少..帮助吗?

For reference.. this was the query that worked right before i introduced that crazy line: 供参考..这是在我介绍那条疯狂的线之前就起作用的查询:

    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();

Just in case you were wondering how I "resolved" this (it's a very hacky answer.. but oh well you do what you gotta do right?) this is what I got: 以防万一您想知道我是如何“解决”这个问题的(这是一个非常棘手的答案。但是,哦,您做得对吗?),这就是我所得到的:

/**
 * 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM