Laravel 4 - 无法覆盖模型的保存方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18199756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - Trouble overriding model's save method
提问by wkm
I'm trying to override my Post class's save() method so that I can validate some of the fields that will be saved to the record:
我正在尝试覆盖我的 Post 类的 save() 方法,以便我可以验证将保存到记录中的一些字段:
// User.php
<?php
class Post extends Eloquent
{
public function save()
{
// code before save
parent::save();
//code after save
}
}
When I try and run a this method in my unit testing I get the following error:
当我尝试在单元测试中运行此方法时,出现以下错误:
..{"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}}
回答by Andreyco
Create Model.php class which you will extend in another self-validating models
创建 Model.php 类,您将在另一个自验证模型中扩展该类
app/models/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);
}
}
Then, adjust your Post model.
Also, you need to define validation rules for this model.
然后,调整您的 Post 模型。
此外,您需要为此模型定义验证规则。
app/models/Post.php
应用程序/模型/Post.php
class Post extends Model
{
// validation rules
protected static $rules = [
'name' => 'required'
];
}
Controller method
Thanks to Model class, Post model is automaticaly validated on every call to save()
method
控制器方法
感谢 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());
}
This answer is strongly based on Jeffrey Way's Laravel Model Validation packagefor Laravel 4.
All credits to this man!
这个答案强烈基于 Jeffrey Way 的Laravel 4 Laravel 模型验证包。
所有功劳都归功于这个人!
回答by th3d0g
How to override Model::save()
in Laravel 4.1
如何Model::save()
在 Laravel 4.1 中覆盖
public function save(array $options = array())
{
parent::save($options);
}
回答by Rob Gordijn
If you want to overwrite the save() method, it must be identical to the save() method in Model:
如果要覆盖 save() 方法,它必须与 Model 中的 save() 方法相同:
<?php
public function save(array $options = array()) {}
And; you can also hook in the save() call with the Model Events: http://laravel.com/docs/eloquent#model-events
和; 您还可以使用模型事件挂钩 save() 调用:http: //laravel.com/docs/eloquent#model-events