Answer the question
In order to leave comments, you need to log in
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
]);
}
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
]);
}
$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' => 'По дате',
],
],
]);
echo $sort->link('number')
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question