【问题标题】:Laravel 4 - Trouble overriding model's save methodLaravel 4 - 故障覆盖模型保存方法
【发布时间】:2013-08-14 12:26:03
【问题描述】:

我正在尝试覆盖我的 Post 类的 save() 方法,以便我可以验证一些将保存到记录中的字段:

// User.php
<?php

class Post extends Eloquent
{
    public function save()
    {
        // code before save
        parent::save(); 
        //code after save
    }
}

当我尝试在单元测试中运行此方法时,出现以下错误:

..{"error":{"type":"ErrorException","message":"Declaration of Post::save() should be compatible with that of Illuminate\\Database\\Eloquent\\Model::save()","file":"\/var\/www\/laravel\/app\/models\/Post.php","line":4}}

【问题讨论】:

    标签: php model laravel laravel-4


    【解决方案1】:

    创建您将在另一个自我验证模型中扩展的 Model.php 类

    app/models/Model.php

    class Model extends Eloquent {
    
        /**
         * Error message bag
         * 
         * @var Illuminate\Support\MessageBag
         */
        protected $errors;
    
        /**
         * Validation rules
         * 
         * @var Array
         */
        protected static $rules = array();
    
        /**
         * Validator instance
         * 
         * @var Illuminate\Validation\Validators
         */
        protected $validator;
    
        public function __construct(array $attributes = array(), Validator $validator = null)
        {
            parent::__construct($attributes);
    
            $this->validator = $validator ?: \App::make('validator');
        }
    
        /**
         * Listen for save event
         */
        protected static function boot()
        {
            parent::boot();
    
            static::saving(function($model)
            {
                return $model->validate();
            });
        }
    
        /**
         * Validates current attributes against rules
         */
        public function validate()
        {
            $v = $this->validator->make($this->attributes, static::$rules);
    
            if ($v->passes())
            {
                return true;
            }
    
            $this->setErrors($v->messages());
    
            return false;
        }
    
        /**
         * Set error message bag
         * 
         * @var Illuminate\Support\MessageBag
         */
        protected function setErrors($errors)
        {
            $this->errors = $errors;
        }
    
        /**
         * Retrieve error message bag
         */
        public function getErrors()
        {
            return $this->errors;
        }
    
        /**
         * Inverse of wasSaved
         */
        public function hasErrors()
        {
            return ! empty($this->errors);
        }
    
    }
    

    然后,调整您的 Post 模型。
    此外,您需要为此模型定义验证规则。

    app/models/Post.php

    class Post extends Model
    {
        // validation rules
        protected static $rules = [
            'name' => 'required'
        ];
    }
    

    控制器方法
    感谢 Model 类,每次调用 save() 方法时都会自动验证 Post 模型

    public function store()
    {
        $post = new Post(Input::all());
    
        if ($post->save())
        {
            return Redirect::route('posts.index');
        }
    
        return Redirect::back()->withInput()->withErrors($post->getErrors());
    }
    

    此答案强烈基于 Jeffrey Way 为 Laravel 4 编写的 Laravel Model Validation package
    所有功劳归于这个人!

    【讨论】:

    • 谢谢 - 非常有用。
    • 复制很好,因为它是手动编码。感谢分享。
    【解决方案2】:

    如何在 Laravel 4.1 中覆盖 Model::save()

    public function save(array $options = array())
    {
       parent::save($options);
    }
    

    【讨论】:

    • @IdanGozlan 请不要更改某人的代码,如果您认为需要更改,请添加评论。
    • 在 parent::save($options); 之前添加 return;
    【解决方案3】:

    如果要覆盖save()方法,必须和Model中的save()方法一致:

    <?php
    public function save(array $options = array()) {}
    

    并且;您还可以使用模型事件挂钩 save() 调用: http://laravel.com/docs/eloquent#model-events

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 2018-10-07
      • 2018-07-26
      相关资源
      最近更新 更多