0

我有一个resume表(除其他外)有一position_type_id列是position_type表的外键。

resume创建表单上,下拉列表的选项position type从表中正确拉出position_type,并将它们的 id 设置为选择的值。一切似乎进展顺利。

提交表单后,我可以验证这些值是否返回到 POST 变量中的控制器,但是它们没有通过属性填充传递到模型中:

$model->attributes=$_POST['Resume'];

这是create控制器上的方法:

public function actionCreate()
{
    $model=new Resume;

    if(isset($_POST['Resume']))
    {
        $model->attributes=$_POST['Resume'];

        if($model->save())
        {
            $this->redirect(array('view','id'=>$model->id));
        }   
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

这是从相关模型加载键的表单:

echo $form->activeDropDownList(
    $model,
    'position_type_id',
    CHtml::listData(PositionType::model()->findAll(),'id','name')
);

position_type_id是 resume 模型上的键,它携带 PositionType 模型的外键。它在表格和简历模型上的拼写相同。模型上的关系为:'positionType' => array(self::BELONGS_TO, 'PositionType', 'position_type_id'),

我想我可以从 POST 数组中获取每个值并手动设置它们,但看起来这应该“正常工作”。设置属性后的值$model用于手动输入的字段,而对于来自生成的下拉列表的所有字段,则为空白。

以下是实际生成的内容:

<select name="Resume[position_type_id]" id="Resume_position_type_id">
    <option value="1">English Teacher</option>
    <option value="2">School Administrator</option>
</select>
4

1 回答 1

0

我可能是错的,但对我来说,大规模的任务不起作用。在 yii 中,如果我们需要对其执行大量赋值,则需要将属性声明为安全的。

因此,在您的模型Resume中,您是否有以下规则:

public function rules() {
    return array(
        //the other rules ...
        array('position_type_id, others_attributes', 'safe'),
        //the rule above indicate the attributes that can be massively assigned
    );
}

关于“安全”验证规则的 wiki 链接

于 2013-03-17T16:21:18.070 回答