PHP头条
热点:

yii ar 增删改查 操作测试记录,yiiar


亲们, 我是yii小白 不要笑话我奥。今天白天写一个管理模块涉及到 yii ar 下的  curd 操作,做 update 操作时纠结了好久,今天晚上花点时间学习, 下面写下我的测试记录

代码如下:

 1     public function actionIndex(){
 2 
 3         // 写入数据
 4         //yii  ar  curd  之   insert
 5         $_POST['Users']['sex'] = 2;
 6         $_POST['Users']['username'] = 'xiaohua';
 7         $model = new Users;
 8         $model->attributes = $_POST['Users'];
 9         $rt = $model->save();
10 
11         if($rt) {
12             Yii::app()->user->setFlash('success','写入成功了');
13             $this->redirect(array('index/test'));
14         } else {
15             Yii::app()->user->setFlash('error','失败了');
16             $this->redirect(array('index/test'));
17         }
18 
19     
20         // 查询数据
21         //get one data by field
22         $hh = Users::model()->find(
23               'username=:name',array(':name'=>'fzb')
24             );
25         print_r($hh['username']);
26 
27         //get one data by id
28         $hh = Users::model()->findByPk(1);
29         print_r($hh['username']);
30 
31         //get all
32         $hh = Users::model()->findAll();
33         print_r($hh);
34 
35         //  修改数据  
36         // update  one
37         $model = new Users;
38         $count = $model->updateByPk(5,array('username'=>'admin','sex'=>1));
39         if($count) {
40             Yii::app()->user->setFlash('success','修改成功了');
41             $this->redirect(array('index/test'));
42         } else {
43             Yii::app()->user->setFlash('error','修改失败了');
44             $this->redirect(array('index/test'));
45         }
46 
47 
48     }

希望大婶们嘴下留情

我还是小白呢

有不足之处希望大家指出,谢谢!

www.phpzy.comtrue/php/31756.htmlTechArticleyii ar 增删改查 操作测试记录,yiiar 亲们, 我是yii小白 不要笑话我奥。今天白天写一个管理模块涉及到 yii ar 下的 curd 操作,做 update 操作时纠结了好久,今天晚上花点时间学习, 下面...

相关文章

PHP之友评论

今天推荐