yasir011/model

此包最新版本(1.0)没有提供许可证信息。

在保存和更新时具有自动验证的Eloquent模型。

1.0 2013-12-03 17:38 UTC

This package is not auto-updated.

Last update: 2024-10-02 06:28:22 UTC


README

WaaviModel受Eloquent的Ardent和Aware模型验证器启发。以下是一些提供的功能:

- Allows Laravel to query related models.
- AutoValidate on model save.
- Allows for unique constraint in validation rules to auto ignore the current model when updating
- Flashes input data when a save fails.

设置

编辑composer.json

"require": {
	"Yasir011/model": "dev-master"
},

模型必须扩展Yasir011\Model\WaaviModel类

<?php

	use Yasir011\Model\WaaviModel;

	class MyModel extends WaaviModel {

		...

	}

验证

WaaviModel使用Laravel的Validator类,因此验证规则、自定义消息和自定义验证方法都是可用的。

use Yasir011\Model\WaaviModel;

class Post extends WaaviModel {

	protected $table = 'posts';

	/**
   * Validation rules
   *
   * @var array
   */
  public $rules = array(
  	'title'	=> 'required|between:5,160',
  	'slug'	=> 'required|unique:posts',
  	'text'	=> 'required',
  );

  /**
   * Validation custom messages
   *
   * @var array
   */
  public $rules = array(
  	'required' => 'The :attribute field is required.',
  );
}

所有Laravel的验证规则都可用,以及您已设置的任何自定义验证规则。在保存模型时,验证会自动运行。可以通过 $model->errors() 获取验证错误。使用示例

	$input = Input::all();
	$success = Post::fill($input)->save();
	if ($success) {
		return Redirect::route('...');
	} else {
		return Redirect::route('...')->withErrors($article->errors());
	}

如果您想跳过验证,可以使用 $model->forceSave();。您可以使用 $model->isValid(); 在保存之前检查模型是否有效。

可以使用以下方式访问和修改运行时的规则:

$rules = array('title'	=> 'required|between:5,160');

$model->getRules();                                 // Returns the model's validation rules.
$model->setRules($rules);                           // Switches the model's validation rules
$model->setRule('slug', 'required|unique:posts');   // Adds or replaces a validation rule
$$model->removeRule('text');                        // Removes rules for a field

自定义消息以相同的方式访问

$messages = array('required'	=> 'The :attribute field is required.');

$model->getCustomMessages();                                    // Returns the model's custom messages.
$model->setCustomMessages($messages);                           // Switches the model's custom messages.
$model->setCustomMessage('required', ':attribute is required'); // Adds or replaces a custom messages.
$$model->removeCustomMessage('required');                       // Removes a custom message.

查询相关模型

WaaviModel扩展了Eloquent查询相关模型的能力。这在查询需要动态构建且需要访问相关模型中的值时非常有用。

让我们通过一个例子来解释。假设您有以下模型

class User extends WaaviModel {
	public function posts() {
		return $this->hasMany('Post');
	}
}

class Post extends WaaviModel {
	public function author() {
		return $this->belongsTo('User');
	}
	public function comments() {
		return $this->hasMany('Comment');
	}
	public function tags() {
		return $this->hasMany('Comment');
	}
}

class Tag extends WaaviModel {
	public function posts() {
		return $this->hasMany('Post');
	}
}

class Comment extends WaaviModel {
	public function post() {
		return $this->belongsTo('User');
	}
}

假设您想获取所有由名为John或Jane的用户创建并被标记为'新闻'的帖子。使用Eloquent来做这个可能会很困难,但使用WaaviModel,您有一个替代方案。您可以通过调用 whereRelated($relationshipName, $column, $operator, $value) 来过滤结果

$posts = Post::whereRelated('tags', 'value', '=', 'News')
  ->whereRelated(function($query)
  {
    $query->whereRelated('author', 'name', '=', 'John')
      ->orWhereRelated('author', 'name', '=', 'Jane');
	})
  ->get();

如果您想检索除John之外所有人的帖子,这同样简单。

$posts = Post::whereNotRelated('author', 'name', '=', 'John')->get()

那么获取John发布的所有文章的评论怎么办呢?

$comments = Comment::whereRelated('post.author', 'name', '=', 'John')->get()

WaaviModel支持Eloquent支持的所有关系,包括MorphOne和MorphMany。可用的过滤器列表如下

WaaviModel::whereRelated($relationshipName, $column, $operator, $value);

WaaviModel::orWhereRelated($relationshipName, $column, $operator, $value);

WaaviModel::whereNotRelated($relationshipName, $column, $operator, $value);

WaaviModel::orWhereNotRelated($relationshipName, $column, $operator, $value);

闭包以及深度关系也受支持,如您在前面的示例中看到的。但是,子查询尚不支持,且使用闭包除外时,必须指定$column、$operator和$value。

whereRelated方法的调用在内部编译为whereIn、whereNotIn或whereNull(当没有记录满足约束时)。为此,每次调用都会查询数据库以查看哪些记录符合指定的约束。这有几个缺点

- Most common databases allow for a limited number of elements (around 10.000) in whereIn clauses. Laravel has the same limitation when using eager loading, which is done in the same way.
- Each whereRelated call queries the database separately, so heavy use will impact performance.