Skip to content

Commit

Permalink
First proposal for solving Query poblem
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammaye committed Jan 9, 2017
1 parent fadc0df commit dd5363e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use MongoDB\BSON\Regex;
use MongoDB\Driver\Cursor as MongoCursor;
use MongoDB\Database as MongoDatabase;

use Yii;
use CComponent;
use CMap;

use sammaye\mongoyii\Client;
use sammaye\mongoyii\Database;
use sammaye\mongoyii\Document;
use sammaye\mongoyii\Cursor;
use sammaye\mongoyii\Exception;
Expand Down Expand Up @@ -62,6 +64,8 @@ class Query extends CComponent
private $_options = ['modifiers' => []];

private $_modelClass;

private $databaseName;

/**
* Constructor.
Expand Down Expand Up @@ -329,9 +333,27 @@ public function getDbConnection()
));
}

public function setDb($db)
{
if($db instanceof Database){
$this->databaseName = $db->database->getDatabaseName();
}elseif($db instanceof MongoDatabase){
$this->databaseName = $db->getDatabaseName();
}else{
$this->databaseName = $db;
}
}

public function getDb()
{
return $this->getDbConnection()->selectDatabase();
$name = $this->databaseName;
if($this->model){
// Get DB name from model
$name = $this->model->getCollection()->database->getDatabaseName();
}

// Will select default database if name is null
return $this->getDbConnection()->selectDatabase($name);
}

/**
Expand Down

0 comments on commit dd5363e

Please sign in to comment.