/**
  * Returns the MongoDB instance for a class.
  *
  * @param string $className The class name.
  * @return Doctrine\ODM\MongoDB\MongoDB
  */
 public function getDocumentDB($className)
 {
     $db = $this->_metadataFactory->getMetadataFor($className)->getDB();
     $db = $db ? $db : $this->_config->getDefaultDB();
     $db = $db ? $db : 'doctrine';
     $db = sprintf('%s%s', $this->_config->getEnvironmentPrefix(), $db);
     if ($db && !isset($this->_documentDBs[$db])) {
         $database = $this->_mongo->selectDB($db);
         $this->_documentDBs[$db] = new MongoDB($database);
     }
     if (!isset($this->_documentDBs[$db])) {
         throw MongoDBException::documentNotMappedToDB($className);
     }
     return $this->_documentDBs[$db];
 }
Ejemplo n.º 2
0
 /**
  * Returns the MongoCollection instance for a class.
  *
  * @param string $className The class name.
  * @return Doctrine\ODM\MongoDB\MongoCollection
  */
 public function getDocumentCollection($className)
 {
     $metadata = $this->_metadataFactory->getMetadataFor($className);
     $collection = $metadata->getCollection();
     $db = $metadata->getDB();
     $key = $db . '.' . $collection;
     if ($collection && !isset($this->_documentCollections[$key])) {
         if ($metadata->isFile()) {
             $collection = $this->_mongo->selectDB($metadata->getDB())->getGridFS($collection);
         } else {
             $collection = $this->_mongo->selectDB($metadata->getDB())->selectCollection($collection);
         }
         $mongoCollection = new MongoCollection($collection, $metadata, $this);
         $this->_documentCollections[$key] = $mongoCollection;
     }
     if (!isset($this->_documentCollections[$key])) {
         throw MongoDBException::documentNotMappedToCollection($className);
     }
     return $this->_documentCollections[$key];
 }