public function actionTest1() { //查询数据 //$id='1 or 1=1'; //$sql='select * from test1 where id=:id'; //$results=Test1::findBySql($sql,array(':id'=>'1 or 1=1'))->all(); //print_r($results); $res = Test1::find()->where(['id' => 1])->all(); //id=1 $res = Test1::find()->where(['>', 'id', 0])->all(); //id>0 $res = Test1::find()->where(['between', 'id', 1, 2])->all(); //id>=1并且id<=2 $res = Test1::find()->where(['like', 'title', 'title2'])->all(); //title like %title2% //查询结果转换成数组 $res = Test1::find()->where(['between', 'id', 1, 2])->asArray()->all(); //批量查询 /*foreach (Test1::find()->asArray()->batch(1) as $tests) { echo $tests; }*/ //删除单数据 //Test1::deleteAll('id=:id',array(':id'=>2)); //插入单数据 $test = new Test1(); $test->id = ''; $test->title = '*****@*****.**'; $test->validate(); //验证数据格式 if ($test->hasErrors()) { //判定是否出现错误 echo "data is error!"; die; } else { echo "data is successful!"; $test->save(); } //修改单数据 /* $test=Test1::find()->where(['id'=>4])->one(); $test->title='title4'; $test->save(); print_r($test);*/ //$this->render('index',$res); }
/** * Finds the Test1 model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Test1 the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Test1::findOne($id)) !== null) { return $model; } else { $this->setHeader(400); echo json_encode(array('status' => 0, 'error_code' => 400, 'message' => 'Bad request'), JSON_PRETTY_PRINT); exit; } }