Yii ar Additions and deletions to check operation Test record
Dear friends, I am yii small white don't laugh at me ao. Today to write a management module related to Yii ar curd operation, do update operation Tangled for a long time, this evening take a moment to learn, write down my test record
The code is as follows:
1 Public functionActionindex () {2 3 //Write Data4 //yii ar curd Insert5 $_post[' Users '] [' sex '] = 2;6 $_post[' Users '] [' username '] = ' Xiaohua ';7 $model=NewUsers;8 $model->attributes =$_post[' Users '];9 $rt=$model-save ();Ten One if($rt) { AYii::app ()->user->setflash (' Success ', ' Write succeeded '); - $this->redirect (Array(' Index/test ')); -}Else { theYii::app ()->user->setflash (' Error ', ' failed '); - $this->redirect (Array(' Index/test ')); - } - + - //Querying Data + //get one data by field A $hh= Users::model ()Find ( at' Username=:name ',Array(': Name ' = ' FZB ')) - ); - Print_r($hh[' username ']); - - //get one data by ID - $hh= Users::model ()->findbypk (1); in Print_r($hh[' username ']); - to //Get All + $hh= Users::model ()findAll (); - Print_r($hh); the * //Modifying Data $ //Update OnePanax Notoginseng $model=NewUsers; - $count=$model->UPDATEBYPK (5,Array(' username ' = ' admin ', ' sex ' =>1)); the if($count) { +Yii::app ()->user->setflash (' Success ', ' modified successfully '); A $this->redirect (Array(' Index/test ')); the}Else { +Yii::app ()->user->setflash (' Error ', ' modification failed '); - $this->redirect (Array(' Index/test ')); $ } $ - -}
I hope the ladies have mercy.
I'm still a little white.
There are shortcomings in the hope that everyone points out, thank you!