小程府 2016-04-11 15:17:09 10033次浏览 3条评论 1 0 0

yii的构造函数方法跟thinkphp差不多,只不过得先定义一个属性,直接贴代码:

public function init(){
      parent::init();

      $this->dish = new Dish();
    }

    public function actionIndex($id)
    {
      $storeid=Yii::$app->request->get('id');
      $where['storeid'] = $storeid;
      $count=$this->dish->find()->where($where)->count();
      $page=new Pagination(['defaultPageSize'=>20,'totalCount'=>$count]);
      $msgs=$this->dish->find()->where($where)->orderBy('dishid desc')->orderBy('status asc')->offset($page->offset)->limit($page->limit)->all();
      return $this->render('index',['page'=>$page,'msgs'=>$msgs]);
    }

还没有完,要在开头public一下哦:代码如下:

class DishController extends Controller
{
    /**
     * @inheritdoc
     */
    public $dish;
您需要登录后才可以评论。登录 | 立即注册