我有这个奇怪的问题,值似乎在表单提交上发布,但控制器似乎找不到它。我用一个表单保存了三个模型。与主模型的关系都是 HAS_MANY。这是控制器。
$model=$this->loadModel($id);
$modelunitgroup = $model->userUnitgroups;
$modelunit = $model->userUnits;
if(isset($_POST['User'], $_POST['Userunitgroup'], $_POST['Userunit']))
{
$model->attributes=$_POST['User'];
$modelunit->attributes=$_POST['Userunitgroup'];
$modelunitgroup->attributes=$_POST['Userunit'];
$valid=$model->validate();
if($valid)
{
if($model->save(false))
{
$modelunitgroup->user_id = $model->id;
$modelunit->user_id = $model->id;
if ($modelunitgroup->save() && $modelunit->save())
{
Yii::app()->user->setFlash('success', "User updated!");
$this->redirect(array('view','id'=>$model->id));
}
}
}
}
$this->render('update',array(
'model'=>$model,
'modelunitgroup'=>$modelunitgroup,
'modelunit'=>$modelunit,
));
这是关系
'userUnitgroups' => array(self::HAS_MANY, 'Userunitgroup', 'user_id'),
'userUnits' => array(self::HAS_MANY, 'Userunit', 'user_id'),
和表单视图
<?php echo $form->errorSummary($model); ?>
<?php echo $form->dropDownListRow($model, 'title', $model->getUtitle(),array('prompt' => 'Select ...'));?>
<?php echo $form->textFieldRow($model,'firstname',array('class'=>'span5','maxlength'=>60)); ?>
<?php echo $form->textFieldRow($model,'lastname',array('class'=>'span5','maxlength'=>60)); ?>
<?php echo $form->textFieldRow($model,'mobile',array('class'=>'span5')); ?>
<?php echo $form->textFieldRow($model,'email',array('class'=>'span5','maxlength'=>160)); ?>
<?php echo $form->passwordFieldRow($model,'password',array('class'=>'span5','maxlength'=>32)); ?>
<?php
foreach ($modelunitgroup as $mgroup) {
echo $form->dropDownListRow($mgroup,
'unitgroup',
$model->getUnitgroups(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('user/getunit'),
'update'=>'#Userunit_unit',
)));
}
foreach ($modelunit as $munit) {
echo $form->dropDownListRow($munit,
'unit',
array()
);
}
?>
我不断收到错误“尝试分配非对象的属性”。似乎从未发布过“Userunitgroup”和“Userunit”,这很奇怪,因为我检查了 Firefox 中的标题并且所有值都正确发布了。有关可能导致此问题的原因以及如何解决此问题的任何帮助?