Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ziaratban committed Jul 16, 2020
1 parent dfe2aa5 commit 70d76cc
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public function init()
$this->trigger(self::EVENT_INIT);
}

/**
* returns mongodb connection object from `modelClass` if `$db` is null otherwise returns `$db`
* @param null|Connection your custom connection object
* @return Connection returns a connection object base on input
*/
protected function finalConObj($db){
$modelClass = $this->modelClass;
return $db === null ? $modelClass::getDb() : $db;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -133,7 +143,7 @@ public function prepare()
*/
public function all($db = null)
{
return parent::all($db === null ? $this->modelClass::GetDb() : null);
return parent::all($this->finalConObj($db));
}

/**
Expand All @@ -146,7 +156,7 @@ public function all($db = null)
*/
public function one($db = null)
{
$row = parent::one($db === null ? $this->modelClass::GetDb() : null);
$row = parent::one($this->finalConObj($db));
if ($row !== false) {
$models = $this->populate([$row]);
return reset($models) ?: null;
Expand All @@ -167,7 +177,7 @@ public function one($db = null)
*/
public function modify($update, $options = [], $db = null)
{
$row = parent::modify($update, $options, $db === null ? $this->modelClass::GetDb() : null);
$row = parent::modify($update, $options, $this->finalConObj($db));
if ($row !== null) {
$models = $this->populate([$row]);
return reset($models) ?: null;
Expand All @@ -182,15 +192,12 @@ public function modify($update, $options = [], $db = null)
*/
public function getCollection($db = null)
{
/* @var $modelClass ActiveRecord */
if ($db === null) {
$db = $this->modelClass::getDb();
}
if ($this->from === null) {
$this->from = $this->modelClass::collectionName();
$modelClass = $this->modelClass;
$this->from = $modelClass::collectionName();
}

return $db->getCollection($this->from);
return $this->finalConObj($db)->getCollection($this->from);
}

/**
Expand Down

0 comments on commit 70d76cc

Please sign in to comment.