簡體   English   中英

Laravel 4 - 麻煩覆蓋模型的保存方法

[英]Laravel 4 - Trouble overriding model's save method

我試圖覆蓋我的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}}

創建Model.php類,您將在另一個自我驗證模型中擴展它

應用程序/模型/ 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模型。
此外,您還需要為此模型定義驗證規則。

應用程序/模型/ 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模型驗證包
這個男人的所有功勞!

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

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

如果要覆蓋save()方法,它必須與Model中的save()方法相同:

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

和; 您還可以使用模型事件掛鈎save()調用: http//laravel.com/docs/eloquent#model-events

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM