Laravel 4 - 故障覆盖模型保存方法

时间:2013-08-13 02:35:33

标签: php model laravel laravel-4

我正在尝试覆盖我的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}}

3 个答案:

答案 0 :(得分:20)

创建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.php中

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

控制器方法
多亏了Model类,Post模型会在每次调用save()方法

时自动验证
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 Model Validation package Laravel 4 这个男人的所有功劳!

答案 1 :(得分:12)

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

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

答案 2 :(得分:8)

如果要覆盖save()方法,它必须与Model中的save()方法相同:

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

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