我有問題,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,
));
}
查看:
哪些變量沒有預期值?預期哪些值? –
當我點擊保存時,(姓,名,中間名,手機,電子郵件)的值不保存。 –
這是Yii 1,對吧? – Eduardo