Beispiel #1
0
 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);
 }