src/Db/Orm/ActiveQuery.php
protected function queryScalarAsync($selectExpression, $db)
    {
        if (
            !$this->distinct
            && empty($this->groupBy)
            && empty($this->having)
            && empty($this->union)
        ) {
            $select = $this->select;
            $order = $this->orderBy;
            $limit = $this->limit;
            $offset = $this->offset;

            $this->select = [$selectExpression];
            $this->orderBy = null;
            $this->limit = null;
            $this->offset = null;
            return $this->createCommand($db)->thenLazy(
                function(CommandInterface $command) use (&$select, &$order, &$limit, &$offset) {
                    $this->select = $select;
                    $this->orderBy = $order;
                    $this->limit = $limit;
                    $this->offset = $offset;
                    return $command->queryScalar();
                }
            );
        }

        $command = (new Query())
            ->select([$selectExpression])
            ->from(['c' => $this])
            ->createCommand($db);
        $this->setCommandCache($command);

        return $command->queryScalar();
    }
protected function queryScalarAsync($selectExpression, $db)
    {
        if (
            !$this->distinct
            && empty($this->groupBy)
            && empty($this->having)
            && empty($this->union)
        ) {
            $select = $this->select;
            $order = $this->orderBy;
            $limit = $this->limit;
            $offset = $this->offset;

            $this->select = [$selectExpression];
            $this->orderBy = null;
            $this->limit = null;
            $this->offset = null;
            return $this->createCommand($db)->thenLazy(
                function(CommandInterface $command) use (&$select, &$order, &$limit, &$offset) {
                    $this->select = $select;
                    $this->orderBy = $order;
                    $this->limit = $limit;
                    $this->offset = $offset;
                    return $command->queryScalar();
                }
            );
        }

        $command = (new Query())
            ->select([$selectExpression])
            ->from(['c' => $this])
            ->createCommand($db);
        $this->setCommandCache($command);

        return $command->queryScalar();
    }
Queries a scalar value by setting [[select]] first.
Restores the value of select to make this query reusable.
@param string|ExpressionInterface $selectExpression
@param DatabaseInterface|DbConnectionGetterInterface|null $db
@return LazyPromiseInterface with bool|string