php - Yii - 1 form 2 models in one view -
i have maintenance controller attributes of students , instructor , make in 1 view values comes student , instructor model not save.
model : student
public function tablename() { return 'student'; } /** * @return array validation rules model attributes. */ public function rules() { // note: should define rules attributes // receive user inputs. return array( array('studentid', 'required'), array('studentid', 'unique'), array('studentid, year, cellphonenumber', 'numerical', 'integeronly'=>true), array('lastname, firstname, middlename, course, email', 'length', 'max'=>32), // following rule used search(). // @todo please remove attributes should not searched. array('studentid, lastname, firstname, middlename, course, year, cellphonenumber, email', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // note: may need adjust relation name , related // class name relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=>label) */ public function attributelabels() { return array( 'studentid' => 'studentid', 'lastname' => 'lastname', 'firstname' => 'firstname', 'middlename' => 'middlename', 'course' => 'course', 'year' => 'year', 'cellphonenumber' => 'cellphonenumber', 'email' => 'email', ); } /** * retrieves list of models based on current search/filter conditions. * * typical usecase: * - initialize model fields values filter form. * - execute method cactivedataprovider instance filter * models according data in model fields. * - pass data provider cgridview, clistview or similar widget. * * @return cactivedataprovider data provider can return models * based on search/filter conditions. */ public function search() { // @todo please modify following code remove attributes should not searched. $criteria=new cdbcriteria; $criteria->compare('studentid',$this->studentid); $criteria->compare('lastname',$this->lastname,true); $criteria->compare('firstname',$this->firstname,true); $criteria->compare('middlename',$this->middlename,true); $criteria->compare('course',$this->course,true); $criteria->compare('year',$this->year); $criteria->compare('cellphonenumber',$this->cellphonenumber); $criteria->compare('email',$this->email,true); return new cactivedataprovider($this, array( 'criteria'=>$criteria, )); } /** * returns static model of specified ar class. * please note should have exact method in cactiverecord descendants! * @param string $classname active record class name. * @return student static model class */ public static function model($classname=__class__) { return parent::model($classname); }
}
model : instructor
public function tablename() { return 'instructor'; }
/** * @return array validation rules model attributes. */ public function rules() { // note: should define rules attributes // receive user inputs. return array( array('instructorid', 'required'), array('cellphone', 'numerical', 'integeronly'=>true), array('instructorid', 'length', 'max'=>16), array('lastname, firstname, middlename, email', 'length', 'max'=>32), // following rule used search(). // @todo please remove attributes should not searched. array('instructorid, lastname, firstname, middlename, cellphone, email', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // note: may need adjust relation name , related // class name relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=>label) */ public function attributelabels() { return array( 'instructorid' => 'instructorid', 'lastname' => 'lastname', 'firstname' => 'firstname', 'middlename' => 'middlename', 'cellphone' => 'cellphone', 'email' => 'email', ); } /** * retrieves list of models based on current search/filter conditions. * * typical usecase: * - initialize model fields values filter form. * - execute method cactivedataprovider instance filter * models according data in model fields. * - pass data provider cgridview, clistview or similar widget. * * @return cactivedataprovider data provider can return models * based on search/filter conditions. */ public function search() { // @todo please modify following code remove attributes should not searched. $criteria=new cdbcriteria; $criteria->compare('instructorid',$this->instructorid,true); $criteria->compare('lastname',$this->lastname,true); $criteria->compare('firstname',$this->firstname,true); $criteria->compare('middlename',$this->middlename,true); $criteria->compare('cellphone',$this->cellphone); $criteria->compare('email',$this->email,true); return new cactivedataprovider($this, array( 'criteria'=>$criteria, )); } /** * returns static model of specified ar class. * please note should have exact method in cactiverecord descendants! * @param string $classname active record class name. * @return instructor static model class */ public static function model($classname=__class__) { return parent::model($classname); }
}
controller : maintenance
public function filters() { return array( 'accesscontrol', // perform access control crud operations 'postonly + delete', // allow deletion via post request ); } /** * specifies access control rules. * method used 'accesscontrol' filter. * @return array access control rules */ public function accessrules() { return array( array('allow', // allow users perform 'index' , 'view' actions 'actions'=>array('index','view','users','createusers'), 'users'=>array('*'), ), array('allow', // allow authenticated user perform 'create' , 'update' actions 'actions'=>array('create','update'), 'users'=>array('*'), ), array('allow', // allow admin user perform 'admin' , 'delete' actions 'actions'=>array('admin','delete'), 'users'=>array('admin'), ), array('deny', // deny users 'users'=>array('*'), ), ); } /** * displays particular model. * @param integer $id id of model displayed */ public function actionview($id) { $this->render('view',array( 'model'=>$this->loadmodel($id), )); } /** * creates new model. * if creation successful, browser redirected 'view' page. */ public function actioncreate() { $model=new users; // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['users'])) { $model->attributes=$_post['users']; if($model->save()) $this->redirect(array('view','id'=>$model->username)); } $this->render('create',array( 'model'=>$model, )); } /** * updates particular model. * if update successful, browser redirected 'view' page. * @param integer $id id of model updated */ public function actionupdate($id) { $model=$this->loadmodel($id); // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['users'])) { $model->attributes=$_post['users']; if($model->save()) $this->redirect(array('view','id'=>$model->username)); } $this->render('update',array( 'model'=>$model, )); } /** * deletes particular model. * if deletion successful, browser redirected 'admin' page. * @param integer $id id of model deleted */ public function actiondelete($id) { $this->loadmodel($id)->delete(); // if ajax request (triggered deletion via admin grid view), should not redirect browser if(!isset($_get['ajax'])) $this->redirect(isset($_post['returnurl']) ? $_post['returnurl'] : array('admin')); } /** * lists models. */ public function actionindex() { $dataprovider=new cactivedataprovider('users'); $this->render('index',array( 'dataprovider'=>$dataprovider, )); } /** * manages models. */ public function actionadmin() { $model=new users('search'); $model->unsetattributes(); // clear default values if(isset($_get['users'])) $model->attributes=$_get['users']; $this->render('admin',array( 'model'=>$model, )); } /** * returns data model based on primary key given in variable. * if data model not found, http exception raised. * @param integer $id id of model loaded * @return users loaded model * @throws chttpexception */ public function loadmodel($id) { $model=users::model()->findbypk($id); if($model===null) throw new chttpexception(404,'the requested page not exist.'); return $model; } /** * performs ajax validation. * @param users $model model validated */ protected function performajaxvalidation($model) { if(isset($_post['ajax']) && $_post['ajax']==='users-form') { echo cactiveform::validate($model); yii::app()->end(); } } //new action url public function actionusers() { // $model=new users('search'); $vm = (object) array(); $vm->users=new users('search'); $vm->student=new student('search'); $vm->instructor=new instructor('search'); $this->render('users',array( 'vm'=>$vm, )); } public function actioncreateusers() { $vm = (object) array(); $vm->users=new users; $vm->student=new student; $vm->instructor=new instructor; $holder; // $model=new users; // uncomment following line if ajax validation needed // $this->performajaxvalidation($model); if(isset($_post['users'])) { $vm->users->attributes=$_post['users']; $vm->users->save(); if(isset($_post['student'])) { $vm->student->attributes=$_post['student']; $vm->student->studentid = $vm->users->username; if($vm->student->save()) $vm->student->unsetattributes(); else{ print_r($vm->student->geterrors());die;// errors } } if(isset($_post['instructor'])) { $vm->instructor->attributes=$_post['instructor']; $vm->instructor->instructorid = $vm->users->username; if($vm->instructor->save()) $vm->instructor->unsetattributes(); else{ print_r($vm->instructor->geterrors());die;// errors } } else { return false; } } // echo 'saved'; // $this->redirect(array('view','id'=>$model->username)); $vm->users->unsetattributes(); $vm->student->unsetattributes(); $vm->instructor->unsetattributes(); $this->render('users',array( 'vm'=>$vm, )); }
}
maintenance : _form enter image description here
hi rules creating problem of dublication
array('studentid', 'unique'),// remove if didn't want
Comments
Post a Comment