2013-08-13 9 views
8

Ich versuche, meinen Beitrag Klasse save() -Methode außer Kraft zu setzen, so dass ich einige der Felder zu validieren, die mit dem Datensatz gespeichert werden:Laravel 4 - Fehler oberstes Modell Methode speichern

// User.php 
<?php 

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

Als ich versuchen und diese Methode in meinen Unit-Tests läuft ich die folgende Fehlermeldung erhalten:

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

Antwort

20

erstellen Model.php Klasse, die Sie erstrecken sich in einem anderen Selbst Validierung Modelle

app/models/Model.php

: Sie können auch mit den Modell Events in den Speichern() -Aufruf Haken
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); 
    } 

} 

Passen Sie dann Ihr Post-Modell an.
Außerdem müssen Sie Validierungsregeln für dieses Modell definieren.

app/models/post.php

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

Controller-Methode
Dank Klasse-Modell ist Pfosten Modell bei jedem Aufruf zu save() Methode

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

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

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

automaticaly validiert Diese Die Antwort basiert stark auf Jeffrey Way's Laravel Model Validation package für Laravel 4.
Alle Credits zu diesem Mann!

+1

Dank außer Kraft setzen - sehr nützlich. – Sunil

+0

Es ist gut zu kopieren, weil es manuell Code ist. Danke fürs teilen. – akbarbin

11

Wie Model::save() in Laravel 4,1

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

@IdanGozlan Bitte ändern Sie nicht den Code von jemandem, fügen Sie einen Kommentar hinzu, wenn Sie denken, dass er geändert werden muss. – serakfalcon

+0

add a * return * vor dem Elternteil :: save ($ options); – Heroselohim