src/Db/Orm/ActiveQuery.php
public function createCommand($db = null)
    {
        /* @var $modelClass ActiveRecordInterface */
        $modelClass = $this->modelClass;
        $connection = null;
        if ($db === null) {
            $db = $modelClass::getDb();
        } elseif ($db instanceof DbConnectionGetterInterface && !$db instanceof DatabaseInterface) {
            $connection = $db->getConnection();
            $db = $db->getDb();
        }

        if ($this->sql === null) {
            $sqlParamsPr = $db->getQueryBuilder()->buildAsync($this);
        } else {
            $sqlParamsPr = new LazyPromise(function() { return resolve([$this->sql, $this->params]); });
        }

        return $sqlParamsPr->thenLazy(
            function($data) use (&$db, &$connection) {
                list($sql, $params) = $data;
                $command = $db->createCommand($sql, $params, $connection);
                $this->setCommandCache($command);

                return $command;
            }
        );
    }
public function createCommand($db = null)
    {
        /* @var $modelClass ActiveRecordInterface */
        $modelClass = $this->modelClass;
        $connection = null;
        if ($db === null) {
            $db = $modelClass::getDb();
        } elseif ($db instanceof DbConnectionGetterInterface && !$db instanceof DatabaseInterface) {
            $connection = $db->getConnection();
            $db = $db->getDb();
        }

        if ($this->sql === null) {
            $sqlParamsPr = $db->getQueryBuilder()->buildAsync($this);
        } else {
            $sqlParamsPr = new LazyPromise(function() { return resolve([$this->sql, $this->params]); });
        }

        return $sqlParamsPr->thenLazy(
            function($data) use (&$db, &$connection) {
                list($sql, $params) = $data;
                $command = $db->createCommand($sql, $params, $connection);
                $this->setCommandCache($command);

                return $command;
            }
        );
    }
Creates a DB command that can be used to execute this query.
@param DatabaseInterface|DbConnectionGetterInterface|null $db the DB connection used to create the DB command.
If `null`, the DB connection returned by [[modelClass]] will be used.
@return LazyPromiseInterface with Command the created DB command instance.