2016-07-25 79 views
0

我有問題,PK的重複,只有用戶保存模型,然後其餘的將0值,後2種形式,在一個視圖

需要幫助的人。由於事先

型號:學生 [編輯]

public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will 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), 
     // The following rule is used by search(). 
     // @todo Please remove those attributes that should not be searched. 
     array('studentid, lastname, firstname, middlename, course, year, cellphonenumber, email', 'safe', 'on'=>'search'), 
    ); 
} 

/** 
* @return array relational rules. 
*/ 
public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the 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 a list of models based on the current search/filter conditions. 
* 
* Typical usecase: 
* - Initialize the model fields with values from filter form. 
* - Execute this method to get CActiveDataProvider instance which will filter 
* models according to data in model fields. 
* - Pass data provider to CGridView, CListView or any similar widget. 
* 
* @return CActiveDataProvider the data provider that can return the models 
* based on the search/filter conditions. 
*/ 
public function search() 
{ 
    // @todo Please modify the following code to remove attributes that should not be 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 the static model of the specified AR class. 
* Please note that you should have this exact method in all your CActiveRecord descendants! 
* @param string $className active record class name. 
* @return Student the static model class 
*/ 
public static function model($className=__CLASS__) 
{ 
    return parent::model($className); 
} 

}

控制器:

公共函數actionCreateUsers() {

$vm = (object) array(); 
    $vm->Users=new Users; 
    $vm->Student=new Student; 
    $vm->Instructor=new Instructor; 
    $holder; 

    // $model=new Users; 
    // Uncomment the following line if AJAX validation is 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(); 
       } 
      if(isset($_POST['Instructor'])) 
       { 
       $vm->Instructor->attributes=$_POST['Instructor']; 
       $vm->Instructor->instructorid = $vm->Users->username; 
        if($vm->Instructor->save()) 
         $vm->Instructor->unsetAttributes(); 
       } 
      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, 
    )); 
} 

查看:

enter image description here

enter image description here

+0

哪些變量沒有預期值?預期哪些值? –

+0

當我點擊保存時,(姓,名,中間名,手機,電子郵件)的值不保存。 –

+0

這是Yii 1,對吧? – Eduardo

回答

0

嗨請提供代碼

正如你說,一個model是節省,但有些則沒有。這可能是model中定義的rules的問題。

嘗試getErrors()如果模型沒有保存

$vm = (object) array(); 
    $vm->Users=new Users; 
    $vm->Student=new Student; 
    $vm->Instructor=new Instructor; 
    $holder; 

    // $model=new Users; 
    // Uncomment the following line if AJAX validation is 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;// to get 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;// to get 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, 
    )); 
} 
+0

Array([studentid] => Array([0] => Studentid「12333」已被採用。))這個錯誤 –

+0

所以你的學生在數據庫中的ID是否是主要的自動增量如果是,那麼刪除它作爲自動增量。如果你想同樣的syudent id插入db – user1234

0

爲了調試,還記得你可以添加:

if($vm->Student->save()) { 
    $vm->Student->unsetAttributes(); 
} else { 
    var_dump($vm->getErrors()); 
    die; 
} 

同爲教師。希望它有幫助

+0

如果($ vm-> Student-> save()){$ {vm-> Student-> unsetAttributes();}其他{ var_dump($ vm-> getErrors()); 死亡; } else語句不工作? 即時對不起,我是新手,需要這個爲我的論文工作 –

+0

如果流沒有進入「其他」如果意味着保存返回true。如果它沒有保存,這意味着你沒有正確設置你的規則,你能分享你的模型嗎? – Eduardo

+0

我更新我的文章並添加學生模型 –