且听风吟 2017-07-17 16:27:12 2740次浏览 3条回复 0 0 0
	 public function actionOrder($id=1)
   {
        $query = Km1::find();
        $count = $query->count();
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => '1']);
        $model = $query->offset($pager->offset)->limit($pager->limit)->all();
        //评论部分
        $id = $model[0]->courid;
        $comments = new Km1Comments();
        if($comments->load(Yii::$app->request->post()) && $comments->saveComments() ){
            return $this->refresh();
        }
        $findcomment = Km1Comments::find()->where("cour_id=:id",[":id"=>$id])->orderBy(['order_id'=>SORT_DESC,'created_time'=>SORT_DESC]);
        $pages = new Pagination(['totalCount' => $findcomment->count(), 'pageSize' => '5']);
        $discuss = $findcomment->offset($pages->offset)->limit($pages->limit)->all();

        $title = '顺序练习';
        //下一题
        $url =  Yii::$app->request->getHostInfo().Yii::$app->request->url.'?page=2&per-page=1';
        $this->layout = 'practice';
        return $this->render('practice',['courses' => $model, 'pager' => $pager,'count'=>$count,'title'=>$title,'comments'=>$comments,'discuss'=>$discuss,'pages'=>$pages,'url'=>$url]);
	  }

下面的另外一个方法

   public function actionRander($id=1)
    {
        $query = Km1::find();
        $count = $query->count();
        $arr = $query->select(['courid'])->column();
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => '1']);

        $result = array_rand($arr,1);
        $model = Km1::findAll($result);

        //评论部分
        $id = $model[0]->courid;
        $comments = new Km1Comments();
        if($comments->load(Yii::$app->request->post()) && $comments->saveComments() ){
            return $this->refresh();
        }
        $findcomment = Km1Comments::find()->where("cour_id=:id",[":id"=>$id])->orderBy(['order_id'=>SORT_DESC,'created_time'=>SORT_DESC]);
        $pages = new Pagination(['totalCount' => $findcomment->count(), 'pageSize' => '5']);
        $discuss = $findcomment->offset($pages->offset)->limit($pages->limit)->all();

        $title = '随机练习';
        //下一题
        $url =  Yii::$app->request->getHostInfo().Yii::$app->request->url.'?page=2&per-page=1';
        $this->layout = 'practice';
        return $this->render('practice',['courses' => $model, 'pager' => $pager,'count'=>$count,'title'=>$title,'comments'=>$comments,'discuss'=>$discuss,'pages'=>$pages,'url'=>$url]);
    }

考虑到有多个这种方法,希望能做一个模板,然后进行调用,这样代码的数量会简化很多。只是网上找了好久都没找到类似的简化方法。。

  • 回复于 2017-07-17 21:40 举报

    用控制器私有方法或模型呀

    3 条回复
    回复于 2017-07-18 08:35 回复

    谢谢大神的指点,控制器私有方法找了半天资料没头绪。只能先用模型分别引用输出了。谢谢你!

    回复于 2017-07-22 10:04 回复

    class private 0.0

    回复于 2017-07-26 08:56 回复

    去年9月份转行开始自学敲代码到现在没满一年,基础很不牢固,见笑了...X)

    觉得很赞
  • 回复于 2017-07-19 11:26 举报

    用ActiveDataProvider

  • 回复于 2017-07-21 18:33 举报

    首先你要创建一个模型kmModel

    $query = Km1::find();
     $dataProvider = new ActiveDataProvider([
         'query' => $query,
         'sort' =>[
             'defaultOrder'=>['created' => SORT_DESC],
          ],
          'pagination' => [
               'page' => $this->page - 1, // oh, shit. this is zero-base
               'pageSize' => $this->pagesize,
           ],
     ])
     $query->select($this->getSelectFields())
         ->andFilterWhere([条件]);
     return $dataProvider;
    
      控制器里这么写:
       $kmModel = new kmModel;
       $dataProvider = $kmModel->search();
       if (empty($dataProvider->getModels())) {
            return '无查询结果';
       }
    
    1 条回复
    回复于 2017-07-26 08:54 回复

    谢谢你! 很有帮助!!!!!非常感谢!

    , 觉得很赞
您需要登录后才可以回复。登录 | 立即注册