在一个视图中发布2个表单

时间:2016-07-25 12:36:39

标签: php yii

我有重复pk的问题,只保存用户模型然后其余的将为0值,

需要帮助的人。提前谢谢

型号:学生 [已编辑]

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

}

控制器:

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

查看:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

嗨请提供代码

正如你所说,一个model正在拯救,但其他人却没有。这可能是rulesmodel定义的问题。

如果型号没有保存,请尝试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,
    ));
}

答案 1 :(得分:0)

为了进行调试,请记住您可以添加:

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

教师也是如此。希望它有所帮助