【问题标题】:yii update and insert scenarios rulesyii 更新和插入场景规则
【发布时间】:2014-09-20 15:53:38
【问题描述】:

我尝试过使用 yii 场景来区分更新和创建新记录之间的规则。当我像下面这样放置场景时,记录不会验证表单并且场景不起作用。我想知道哪里出错了

return array(
            array('first_name, second_name, username, password, email, skype, attachment, just, cv', 'required','on'=>'insert'),
           array('first_name, second_name, username, password, email', 'required','on'=>'update'),
            array('first_name, second_name, username, password, email, skype, status, attachment, just, cv', 'length', 'max'=>255),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('attachment, cv', 'file', 'types'=>'doc, docx, pdf, odt'),
            array('re_password','compare','compareAttribute'=>'password'),

        );

当我不使用场景时

  return array(
                array('first_name, second_name, username, password, email, skype, attachment, just, cv', 'required'),

                array('first_name, second_name, username, password, email, skype, status, attachment, just, cv', 'length', 'max'=>255),
                // The following rule is used by search().
                // @todo Please remove those attributes that should not be searched.
                array('attachment, cv', 'file', 'types'=>'doc, docx, pdf, odt'),
                array('re_password','compare','compareAttribute'=>'password'),

            );

这可行,但有一些列我不需要更新来打扰像 cv 和附件。

由于我遗漏了一些东西,请亮出一些光。

【问题讨论】:

    标签: php yii


    【解决方案1】:

    您需要在使用模型时设置场景。有两种方法可以做到这一点:

    $model=new ModelName('SCENARIO_NAME');
    

    或者

    $model=new ModelName();
    $model->setScenario('SCENARIO_NAME');
    

    【讨论】:

    • 或者只是$model->scenario = "scenario_name"
    • 我不是亲爱的,我是斑马!
    • @Justinas :))) 哈哈,我喜欢这个对话。无论如何,您的替代解决方案是正确的:)
    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2016-10-30
    • 2011-04-16
    • 2019-07-20
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多