How to rewrite the method from Controller to Model, yii2



  • I did the wrong thing, the request was written in the counteraller rack. Like those who rewrite it so that the method itself is in the Content model, and in the action-Album counteraller, it's called and retracted the vogue return $this-staterender('album'). I didn't find an infu on this. Code of Action

    public function actionAlbum($id)
    {
    
    $query = Content::find()
        ->select('content.*')
        ->with('category')
        ->where(['category_id' => $id]);
    
    $pagination = new Pagination(['totalCount' => $query->count(), 'pageSize'=>6]);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => $pagination
    ]);
    return $this->render('album', [
        'dataProvider' => $dataProvider,
        'pagination' => $pagination,
    ]);
    

    }



  • Right or wrong, it's up to you. The information on this subject is about to be called, "How do two methods in two classes and call another one." The accuracy of implementation will depend on needs, a simple example:

    Model:

    class Content extends AcitveRecord{
        ...
    
    public static function GetAlbum($id){
        $query = Content::find();
    
        ....
    
        return $var;
    }
    
    ...
    

    }

    Controller:

    public function actionAlbum($id)
    {

    $data = Content::GetAlbum($id);
    
    ...
    
    return $this->render('album', [
        'data' => $data
    ]);
    

    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2