E
E
evgeniy_matveev2018-07-10 14:00:06
Yii
evgeniy_matveev, 2018-07-10 14:00:06

How to organize sorting in ActiveRecord?

Hello. There is such an action in the project.

public function actionIndex(){
        $pageSize =Yii::$app->request->get('pagesize');
        $users = User::find()->all();
        $query = Projects::find()->where(['status'=>'0'])->with(['materials','design','installing','building']);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>$pageSize,'forcePageParam'=>false,'pageSizeParam'=>false,'defaultPageSize'=>10]);
        $status = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
        return $this->render('index', [
            'status' => $status,
            'pages'=>$pages,
            'users'=>$users
        ]);
    }

The page search is organized as follows:
public function actionSearch(){
        $users = User::find()->all();
        $search = Yii::$app->request->get('search');
        $query = Projects::find()
            ->Where(['projects.status'=>0])
            ->joinWith('materials')
            ->andWhere([
                'or',
                ['like','materials.number',$search],
                ['like','materials.description',$search],
                ['like','projects.number',$search]
            ])
            ->with(['materials','design','installing','building']);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>20,'forcePageParam'=>false,'pageSizeParam'=>false]);
        $status = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
        return $this->render('index', [
            'status' => $status,
            'pages'=>$pages,
            'users'=>$users,
            'search'=>$search
        ]);
    }

At the same time, there is a need to implement sorting. Please tell me how to sort records without using a repeated query to the database. Sorting in the available selection.
Or prompt the mechanism which allows using request for search in a DB to "glue" sorting. Something like this. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2018-07-10
@evgeniy_matveev

Sorting , but it's better to make a normal data provider out of all this gut

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question