php - 更新时,跳过更新 yii 的某些属性

标签 php yii model

即使那些设置为 POST 数组,我也需要停止更新某些值。为此,我在 yii 规则中使用了 unsafe。

array('id', 'unsafe', 'on'=>'update'),

仍然如此,我无法跳过 ID 更新。

如何使用 yii 完成此操作?

下面是我的规则函数..

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('name, body, created_date', 'required'),
        array('name', 'length', 'max'=>128),
        array('body', 'length', 'max'=>512),
        array('id', 'unsafe', 'on'=>'update'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, name, body, created_date', 'safe', 'on'=>'search'),
    );
}

更新 1

$model->attributes = $_POST['U​​ser'];

我需要在保存时跳过某些属性。

$模型->保存();

最佳答案

当您在 Controller 中创建新模型实例时,您需要声明场景 例如 如果你的声明是这样的

$myModelInstance = new MyModel();

你需要把它改成

$myModelInstance = new MyModel('update');

但是,如果您使用事件记录的一种查找方法来保存它,那么它会自动设置为“更新”,如下所示:http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail

如果您使用其他一些逻辑来声明模型,您可以简单地使用 setScenario 函数

$myModel->setScenario("update"); 

关于php - 更新时,跳过更新 yii 的某些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114179/

相关文章:

php - FirePHP 不输出任何东西

php - Yii 框架的 Yiistrap 中的 Bootstrap 路径和 CDN URL 无效

debugging - 使用 netbeans 调试 yii 控制台应用程序

ruby-on-rails-3 - Rails 3 针对一列验证一个值的唯一性?

php - CakePhp:Cake Email AfterSend 事件

php - 在 PHP 问题中通过套接字写入然后读回

php - 在 Yii2 的 Controller 中同时使用 beforeAction() 和 behavior() 方法

php - 服务层和模型与领域驱动设计的关联

ruby-on-rails - rails : call method within model

php - 连接到 CommissionJunction token 时出错 : [60] SSL certificate problem: unable to get local issuer certificate