YII 框架常用数据库操作语法 /********************数据库操作的相关语法**************/ /********************查询**************/ $collect = $model->find(array('condition'=>'status > 0 ','order'=>'id desc','limit'=>5)); $collect->addtime; Yii::app()->db->createCommand($sql)->queryAll(); Yii::app()->db->createCommand($sql)->queryRow(); Yii::app()->db->createCommand($sql)->execute(); /********************增加**************/ $model = new Industry(); $model->setAttribute('name', ""); $model->setAttribute('addtime', time()); if($model->save()){ $this->redirect('/seller/industry/index/'); Yii::app()->end(); } /****************************统计*******************/ $num = $this->count(array("condition"=>"uid={$id} and token={$token}")); //获取上次插入记录后的id Yii::app()->db->getLastInsertID(); /********************修改**************/ //第一种方式 $user = new User; $userinfo['updatetime'] = time(); $rs=$user->updateAll($userinfo, 'id=:user_id', array(':user_id'=>$user_id)); //第二种方式 public function actionUpdate($id){ $model = $this->loadModel($id); $model->setAttribute('name', ""); $model->setAttribute('addtime', time()); if($model->save()){ $this->redirect('/seller/industry/index/'); Yii::app()->end(); } } public function loadModel($id) { $project=new Industry(); $model = $project->findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } //处理报错 print_r($model->getErrors()); //获取域名 Yii::app()->request->hostInfo //获取参数 get或者post传值 Yii::app()->request->getParam('id');