[英]Yii Model has an invalid validation rule. Won't insert to multiple models
我正在嘗試創建一個用戶,並且能夠將其插入到SystemUsers模型和Party模型中,但是現在當我添加代碼以嘗試對Persons模型進行插入時,我得到了錯誤的Persons具有無效的驗證規則。 規則必須指定要驗證的屬性和驗證者名稱。
我的控制器代碼有什么問題嗎?
public function actionCreate()
{
$model = new SystemUsers;
$modelParties = new Parties;
$modelPersons = new Persons;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($invoice);
if (isset($_POST['SystemUsers']))
{
$model->attributes = $_POST['SystemUsers'];
Yii::app()->db->createCommand("insert into parties (party_type_id) values ('1')")->execute();
$id = Yii::app()->db->lastInsertId; //The id from parties is a fk of system users
$model->password = md5($model->password);//this works
$model->party_id = $id; //this works
$modelPersons->party_id = $id; //I believe errors start to occur startin here
$modelPersons->email = $model->username;
$modelPersons->save();
if($model->save())
echo "<script>alert('User Created');</script>";
// }
else $this->redirect(array('views22','id'=>$model->id));
}
$this->render('create',array(
'parties'=>$modelParties,
'model'=>$model,
));
}
這些是“我的人”模型的規則:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('party_id, email, company_name, last_name, middle_name, gender, dob_year, dob_month, dob_day, nickname, language, address, country, location, moderator, from_date_location, to_date_location'),
array('dob_year, dob_month, dob_day, language, country, location', 'numerical', 'integerOnly'=>true),
array('party_id', 'length', 'max'=>20),
array('email', 'length', 'max'=>100),
array('company_name, last_name, middle_name, nickname', 'length', 'max'=>255),
array('gender', 'length', 'max'=>6),
array('sub_admin, moderator', 'length', 'max'=>1),
array('image', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('party_id, email, company_name, last_name, middle_name, gender, image, dob_year, dob_month, dob_day, nickname, language, address, country, location, sub_admin, moderator, from_date_location, to_date_location', 'safe', 'on'=>'search'),
);
}
在規則功能中,您沒有提到第一個數組的規則。 您只是無規則地提及字段名稱。
您必須提及以下required
字段驗證規則
array('party_id, email, company_name, last_name, middle_name, gender, dob_year, dob_month, dob_day, nickname, language, address, country, location, moderator, from_date_location, to_date_location','required'),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.