2015-05-12 10:37:57 12982次浏览 6条回答 1 悬赏 1 金钱
public function actionIndex(){
    $student = new Student();
    if($student->load(Yii::$app->request->post())){
        return $this->render('student_info',['student'=>$student]);
    }else{
        return $this->render('student');
    }
}

这是Controller里面的代码,post()返回的数组我查看了是有值的,load方法也执行过了,但是student对象的属性查看了依然为null,这是为什么?

最佳答案

  • FatrBaby 发布于 2015-05-12 13:12 举报

    发出你视图的代码,这样看不出什么问题

    2 条回复
    回复于 2015-05-12 14:10 回复

    这是表单视图:

    <form action="index.php?r=model" method="post">
        Name:<input name="Student[name]"><br>
        ID:<input name="Student[id]"><br>
        Sex:<input name="Student[sex]"><br>
        Age:<input name="Student[age]"><br>
        <input type="submit" value="submit">
    </form>
    

    这是提价后应该跳转的视图:

    <label>Name:</label><?=$student->name?><br>
    <label>Age:</label><?=$student->age?><br>
    <label>Sex:</label><?=$student->sex?><br>
    <label>ID:</label><?=$student->id?>
    
    回复于 2015-05-13 08:29 回复

    提交后你在哪儿跳转的?你这样肯定不行,你用load对模型赋值后,没有保存再跳转后之肯定都没了。

  • 回答于 2015-09-17 08:54 举报

    我也遇到了这个问题,研究了很长时间,后来决定看源代码。
    打开 vendor/yiisoft/yii2/base/Model.php

    load()方法调用了setAttributes($values, $safeOnly = true)。
    $values就是我们post过来的数据,$safeOnly没有理解,默认true,但是load()在调用setAttributes()没有传递$safeOnly参数,导致默认$safeOnly等于true

    setAttributes()方法中有这样一句
    $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());
    入托$safeOnly = true 调用 $this->safeAttributes(),否则调用$this->attributes()
    这两个方法应该都是返回当前model所定义的字段数组,但是$this->safeAttributes()始终为[],我试了一下$this->attributes()可以正常返回字段数组。

    所以解决办法:
    在load()调用setAttributes()时,传递第二个参数$safeOnly为false,一共两处。

        $scope = $formName === null ? $this->formName() : $formName;
        if ($scope === '' && !empty($data)) {
            $this->setAttributes($data,false);
    
            return true;
        } elseif (isset($data[$scope])) {
            $this->setAttributes($data[$scope],false);
    
            return true;
        } else {
            return false;
        }
    

    再测试成功了。 :)

    我一直在使用Laravel这个框架,非常的好用,而且感觉非常专业bug极少(目前我还没遇到过)。我也是刚接触yii这个框架,第一天就卡在了这里,看着官方教程做的。瞬间对这个框架没什么信心了。

    2 条回复
    回复于 2017-08-22 18:46 回复

    不是bug。首先load()方法是框架里面的方法,实际项目中不是你想改就可以该吧。
    这里的问题在于:
    safeAttributes()方法,$safeOnly参数为true的时候,是一种安全模式:
    A safe attribute is one that is associated with a validation rule in the current [[scenario]].
    意思就是说,安全模式下,要想更改一个属性的值,比如在models中设置rules规则。
    我认为这是一种非常好的思想。想更改一个变量,必须先对变量设置一个规则,然后校验通过了再允许修改。

    回复于 2017-08-25 10:29 回复

    正解,亲测成功!!!

    觉得很赞
  • 回答于 2016-12-30 13:55 举报

    我也一直在查是啥原因,感谢!!

  • 回答于 2017-01-03 11:19 举报

    应该是没用对吧

  • 回答于 2017-11-27 11:56 举报

    load()方法是考虑安全性的,所以load赋值成功的前提是你的model有rules,而且你的属性加了相关的rule
    例如

    <?php
    public function rules()
        {
            return [
                [['search'],'string']
            ];
        }
    ?>
    
  • 回答于 2021-01-14 17:08 举报

    这个问题 也可能是设置了 scenario 后, 但是Model里没有写需要验证的字段

您需要登录后才可以回答。登录 | 立即注册
wenyudengdeng
经理

wenyudengdeng

注册时间:2015-04-26
最后登录:2015-07-15
在线时长:7小时53分
  • 粉丝4
  • 金钱735
  • 威望30
  • 积分1105

热门问题