在yii中修改模型的objectname

时间:2014-04-04 06:14:28

标签: php mysql yii

我有一个模型 Skill.php 。在* SkillController.php * 我有crud生成的actionCreate方法

actionCreate(){
   $model=new Skill;
       }

当我将$ model更改为$ modelSkill时,它显示错误。为什么?

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

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Skill']))
        {
            $model->attributes=$_POST['Skill'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->skill_id));
        }

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

更改为$ modelSkill后

public function actionCreate()
    {
        $modelSkill=new Skill;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($modelSkill);

        if(isset($_POST['Skill']))
        {
            $modelSkill->attributes=$_POST['Skill'];
            if($modelSkill->save())
                $this->redirect(array('view','id'=>$modelSkill->skill_id));
        }

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

1 个答案:

答案 0 :(得分:1)

我想我发现你失败了。您是否也可以在View文件中替换它?如果是的话,你还需要改变这个:

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

到此:

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

正如您在Yii documentation中看到的那样,数组将被放入函数extract()