Mr. chan Mr. chan - 4 months ago 8
PHP Question

post 2 forms in one view

i have problems with duplication of pk, and only users model is saved then the rest will 0 value,

need help guys. thanks in advance

Model: Student [edited]

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


View:

enter image description here

enter image description here

Answer

Hi Please provide the code

As you said that one model is saving but others are not. This may be the issue of rules define in model.

Try to getErrors() if model doesn't save

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