Nestquery Suggestion

Sometimes we need to nest a query, for instance if we want to sort before grouping or if we want to count results from a grouped query.


     * Nests the current query in the from field


    public function nestQuery()


        $command = $this->createCommand();

        $queryObject = call_user_func_array([$this->modelClass, 'find'], []);

        $queryObject->from(['('.$command->sql.') AS t'])


        return $queryObject;


Usage example:

       $query = MenuItemInfo::find()

            ->orderByField('language', ['da','en','de'])





The first nestQuery ensures that the results are grouped after sorting. This is necessary to ensure the prioritization of languages in the example. The second nestQuery prepares the query for pagination - without it, the pagination would return the language count instead of the row count.