2013-08-13 44 views
8

Tôi đang cố gắng để ghi đè phương pháp tiết kiệm lớp bài viết của tôi() để tôi có thể xác nhận một số các lĩnh vực đó sẽ được lưu vào hồ sơ:Laravel 4 - Rắc rối trọng mô hình của phương pháp tiết kiệm

// User.php 
<?php 

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

Khi tôi thử và chạy một phương pháp này trong kiểm tra đơn vị của tôi, tôi nhận được lỗi sau:

..{"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}} 

Trả lời

20

Tạo lớp Model.php mà bạn sẽ kéo dài trong một mô hình tự phê

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); 
    } 

} 

Sau đó, điều chỉnh Mô hình bài đăng của bạn.
Ngoài ra, bạn cần xác định các quy tắc xác thực cho mô hình này.

app/models/Post.php

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

phương pháp điều khiển
Nhờ Xây dựng mô hình lớp học, Bưu mô hình được automaticaly xác nhận trên tất cả các cuộc gọi đến save() phương pháp

public function store() 
{ 
    $post = new Post(Input::all()); 

    if ($post->save()) 
    { 
     return Redirect::route('posts.index'); 
    } 

    return Redirect::back()->withInput()->withErrors($post->getErrors()); 
} 

này câu trả lời dựa trên số Laravel Model Validation package của Jeffrey Way cho Laravel 4.
Tất cả các khoản tín dụng cho người đàn ông này!

+1

Cảm ơn - rất hữu ích. – Sunil

+0

Rất tốt để sao chép vì mã theo cách thủ công. Cảm ơn bạn đã chia sẻ. – akbarbin

8

Nếu bạn muốn ghi đè lên các phương pháp tiết kiệm(), nó phải là giống với phương pháp tiết kiệm() trong mẫu:

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

Và; bạn cũng có thể treo trong save() cuộc gọi với sự kiện mẫu: http://laravel.com/docs/eloquent#model-events

11

Làm thế nào để ghi đè Model::save() trong Laravel 4,1

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

@IdanGozlan Xin vui lòng không thay đổi mã của ai đó, thêm một bình luận nếu bạn nghĩ rằng nó cần phải được thay đổi. – serakfalcon

+0

thêm dấu * * trước cha mẹ :: lưu ($ tùy chọn); – Heroselohim

Các vấn đề liên quan