E
E
evgeniy_matveev2018-07-25 10:15:33
Yii
evgeniy_matveev, 2018-07-25 10:15:33

How to correctly write a method in YII2?

Hello I am new to yii. I started writing my first project with minimal technical specifications. As usual, in the process of TK it swelled up. As a result, I got four very similar methods:
"Index" method

public function actionIndex(){
        $sort = new Sort([
            'attributes' => [
                'number' => [
                    'asc' => ['number' => SORT_ASC],
                    'desc' => ['number' => SORT_DESC],
                    'default' => SORT_ASC,
                    'label' => 'По номеру',
                ],
                'date' => [
                    'asc' => ['date' => SORT_ASC],
                    'desc' => ['date' => SORT_DESC],
                    'default' => SORT_ASC,
                    'label' => 'По дате',
                ],
            ],
        ]);
        $stat = Yii::$app->request->get('stat');
        if (!$stat){
            $stat='0';
        }
        $session = Yii::$app->session;
        $pageSize = Yii::$app->request->get('pagesize');
        if($pageSize){
            $session['pageSize'] = $pageSize;
        }
        $users = User::find()->all();
        $query = Projects::find()->where(['status'=>$stat])->with(['materials','design','installing','building'])->orderBy($sort->orders);

        $countQuery = clone $query;
        $pages = new Pagination([
            'totalCount'=>$countQuery->count(),
            'pageSize'=>$session['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,
            'sort' => $sort
        ]);
    }

Search method:
public function actionSearch(){
        $sort = new Sort([
            'attributes' => [
                'number' => [
                    'asc' => ['number' => SORT_ASC],
                    'desc' => ['number' => SORT_DESC],
                    'default' => SORT_ASC,
                    'label' => 'По номеру',
                ],
                'date' => [
                    'asc' => ['date' => SORT_ASC],
                    'desc' => ['date' => SORT_DESC],
                    'default' => SORT_ASC,
                    'label' => 'По дате',
                ],
            ],
        ]);
        $stat = Yii::$app->request->get('stat');
        if (!$stat){
            $stat='0';
        }
        $session = Yii::$app->session;
        $pageSize = Yii::$app->request->get('pagesize');
        if($pageSize){
            $session['pageSize'] = $pageSize;
        }
        $users = User::find()->all();
        $search = Yii::$app->request->get('search');
        $query = Projects::find()
            ->Where(['status'=>$stat])
            ->joinWith('materials')
            ->andWhere([
                'or',
                ['like','materials.number',$search],
                ['like','materials.description',$search],
                ['like','projects.number',$search]
            ])
            ->with(['materials','design','installing','building'])
            ->distinct()
            ->orderBy($sort->orders);
        /*$countQuery = clone $query;*/
        $pages = new Pagination(['totalCount'=>$query->count(),'pageSize'=>$session['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,
            'search'=>$search,
            'sort'=>$sort
        ]);
    }

Actually, I drew the ideology of separation of search methods from the CRUD module. But, firstly, it turned out a lot of copy-paste.
And secondly, there is a problem with calling these methods from view.
Hence two questions:
1. How to combine these methods?
2. In case you don't combine methods.
The following construction is used for sorting:
$sort = new Sort([
            'attributes' => [
                'number' => [
                    'asc' => ['number' => SORT_ASC],
                    'desc' => ['number' => SORT_DESC],
                    'default' => SORT_ASC,
                    'label' => 'По номеру',
                ],
                'date' => [
                    'asc' => ['date' => SORT_ASC],
                    'desc' => ['date' => SORT_DESC],
                    'default' => SORT_ASC,
                    'label' => 'По дате',
                ],
            ],
        ]);

And in the view, everything is enough and this link will work for all methods. How to make the same construction for pagination? The point is that Pagination() doesn't have attributes.
<?php echo $sort->link('number')?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abdujabbor1987, 2018-07-25
@abdujabbor1987

The very first thing you should probably move all the logic that you wrote on the controller to the model level, then try the DRY principle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question