2016-07-25 13 views
0

Ich habe Probleme mit der Duplizierung von PK, und nur Benutzer Modell wird dann gespeichert, der Rest wird 0 Wert,Post 2 Formulare in einer Ansicht

brauchen Hilfe Jungs. Vielen Dank im Voraus

Modell: Student [bearbeitet]

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); 
} 

}

Controller:

public function 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, 
    )); 
} 

Ausblick:

enter image description here

enter image description here

+0

Welche Variablen haben nicht die erwarteten Werte? Welche Werte werden erwartet? –

+0

Wenn ich auf Speichern klicke, ist der Wert von (Nachname, Vorname, zweiter Vorname, Mobiltelefon, E-Mail) nicht sicher. –

+0

Ist das Yii 1, oder? – Eduardo

Antwort

0

Hallo Bitte geben Sie den Code

Wie Sie gesagt haben, dass man model spart aber andere nicht. Dies kann die rules Definition in model sein.

Versuchen Sie, getErrors() wenn Modell nicht

$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" wurde bereits vergeben .)) dieser Fehler –

+0

So ist Ihre Studenten-ID in der Datenbank primäre Autoinkrement ist oder nicht, wenn ja, dann entfernen Sie das als Autoinkrement. Wenn Sie möchten, dass dieselbe syudent-ID in db eingefügt wird – user1234

0

Um Debuggen nicht speichern, erinnern Sie hinzufügen können:

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

Das Gleiche gilt für Ausbilder. Hoffe es hilft

+0

if ($ vm-> Student-> save()) { $ vm-> Student-> unsetAttributes(); } else { var_dump ($ vm-> getErrors()); sterben; } die Else-Anweisung funktioniert nicht? tut mir leid, ich bin Neuling, muss dies für meine These arbeiten –

+0

Wenn der Fluss ist nicht zu den "sonst", wenn das Speichern der zurückgegeben wahr ist. Wenn es nicht gespeichert wird, heißt das, dass Sie Ihre Regeln nicht korrekt festgelegt haben. Können Sie Ihre Modelle teilen? – Eduardo

+0

Ich aktualisiere meinen Beitrag und füge das Studentenmodell hinzu –

Verwandte Themen