Native Mongolian ODM Doctrine

In the doctrine of relational databases, we have QueryBuilder, it can manually write DQL queries, and if we really must, we can use the Doctrine join to execute raw SQL. I have not found (either in the API or the documentation) a way to do this in the Mongo doctrine project.

How can I execute my own query using mongo odm? (Besides doctrine_mongodb.odm.default_connection injection, or is this really the only way?)

+4
source share
2 answers

Inside the document repository you can add a private method, for example:

private function _getNativeConnection(){
    $connection = $this->getDocumentManager()->getConnection();
    $mongo = $connection->getMongo();
    if(!$mongo){
        $connection->connect();
        $mongo = $connection->getMongo();
    }

    //You can use this as literal strings, or pass them as parameters to the method
    $db = $mongo->selectDB('YOUR_MONGO_DB')->selectCollection("MONGO_COLLECTION");

    return $db;
}

Then you can use it from another repository method, for example:

public function another_public_method{
    ...
    $collection = $this->_getNativeConnection();
    ...
}

$collection PHP MongoCollection (http://php.net/manual/en/class.mongocollection.php), Mongo ( ), , , , , ..

, ?

+2

$collection = $documentManager->getDocumentCollection('Vendor\MyDocument');

,

$collection->aggregate($pipeline);
0

Source: https://habr.com/ru/post/1534611/


All Articles