innoscience / eloquental
一个相当聪明的Laravel Eloquent ORM模型,具有自验证、排序和查询控制功能。
Requires
- php: >=5.3.0
- illuminate/database: ~4.1
- illuminate/support: ~4.1
- illuminate/validation: ~4.1
Requires (Dev)
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-09-28 16:26:14 UTC
README
它的eloquental,我亲爱的华生!一个相当聪明的Laravel Eloquent ORM模型,具有自验证、排序和查询控制功能。
灵感来源于Ardent和Eloquent。
版权(C)2014 Brandon Fenning
义务
测试并兼容PHP 5.3+
需要Laravel 4.1
安装
将 innoscience/eloquental 添加到 composer.json 文件中
"require": {
"innoscience/eloquental": "dev-master"
}
之后,运行 composer update 安装包
简要概述
Eloquental被命名空间到 Innoscience\Eloquental,以下是一个使用条件验证并结合模型验证事件的Eloquental模型的简单示例
use Innoscience\Eloquental\Eloquental;
class News extends Eloquental {
var $table = 'news';
var $orderBy = array('date' => 'desc');
var $rules = array(
'title'=>'required',
'slug'=>'required|unique:news,slug',
'date'=>'required|date',
'article'=>'required|min:50'
);
}
News::validating(function($model){
$model->getValidator()->sometimes('slug', 'required|unique:news,slug,'.$model->id, function($model) {
return $model->exists;
});
});
News::validated(function($model){
// # Do some awesome validated stuff here that I can't think of at the moment
});
// # Elsewhere, our models initiated, the latent eloquental power is harnessed...
$model = new Article(array(
'title'=>'Case Files',
'date'=>'1889-10-01',
'slug'=>'case-files',
'article'=>'My notes are as follows...'));
if (!$model->save()) {
return Redirect::back()->withErrors($model->errors());
}
事物的自然排序
Eloquental有一个内置的排序属性,如果设置,将默认在查询中调用 orderBy()
注意:此功能目前不适用于 $query->lists() 方法
News extends Eloquental {
...
var $orderBy = array('date' => 'desc');
}
为排序带来秩序
添加尽可能多的排序子句,以 $field => $direction 对的形式
User extends Eloquental {
...
var $orderBy = array(
'lastname' => 'asc',
'firstname' => 'asc'
);
}
有趣的事实:当查询模型时添加
->orderBy()子句,模型->orderBy属性将在生成查询时被忽略。这是通过Eloquental提供的新Builder实现的,它允许在查询构建过程的最后有条件地添加orderBy子句。稍后,文档将展示如何将类似的自定义功能添加到您的模型中。
验证此物,我的好先生
基础
Eloquental有一个内置的规则集,并且可以在需要时动态添加规则。
Article extends Eloquental {
...
var $rules = array(
'title'=>'required'
);
}
$model = new Article(array('title'=>'Case Files'));
if (!$model->save()) {
return Redirect::back()->withErrors($model->errors());
}
// Or validate the model without saving
if (!$model->validate()) {
return Redirect::back()->withErrors($model->errors());
}
有趣的事实:如果没有设置
$rules,则模型->validate()方法以及::validating或::validated事件都不会为模型触发。
验证事件
Eloquental模型有两个在保存过程中触发的验证事件
User::validating(function($model) {
$model->getValidator()->sometimes('password', 'required|min:8|confirmed', function($model) {
return ($model->password && $model->isDirty('password')) ? TRUE : FALSE;
});
});
User::validated(function($model) {
// # Validated stuff goes here
});
跳过验证
有时您可能希望模型跳过验证
$model->skipValidation()->save();
一旦在模型上调用 ->save() 方法,skipValidation 标志将返回 FALSE。后续的保存将进行验证,除非再次调用 skipValidation()。
验证错误
如果模型验证失败,错误可以通过 ->errors() 方法访问,它返回Laravel的 MessageBag
if (!$model->save()) {
echo $model->errors()->all('<li>:message</li>');
}
手动验证
if (!$model->validate()) {
echo $model->errors()->all('<li>:message</li>');
}
带有覆盖规则的手动验证
$rules = array('title'=>required');
$customMessages = array('title'=>'This be required');
if (!$model->validate($rules, $customMessages)) {
echo $model->errors()->all('<li>:message</li>');
}
注意:在调用
$model->validate($rule = array(), $customMessages = array())时设置$rules或$customMessages属性将覆盖模型默认规则并重置验证实例。
操作验证器实例
如果您想操作 Validator 实例,您可以轻松做到
$model = News::find(1);
$model->getValidator()->sometimes('author', 'required', function($model) {
return $model->published == 1;
});
设置验证器实例
$model->setValidator(Validator::make($rules, $messages));
自动清除无用的数据
与验证机制结合使用,自动清除允许在保存之前将属性传递给模型并丢弃
User extends Eloquental {
...
var $autoPurge = array('password_confirmation');
}
这将导致在保存之前从模型属性中清除 password_confirmation 属性。
有趣的事实:自动清除的属性在
::validated()事件中最后可访问,因为它们是在保存事件触发之前被清除的。如果没有填充$autoPurge属性,则不会应用自动清除。
及时构建查询
查询构建器事件是一种强大的机制,允许您在查询构建器执行之前有条件地插入查询子句。Eloquental 中的 ->orderBy 机制就是这种功能的实现。其他用途包括根据认证状态有条件地显示模型等。应谨慎使用。
有趣的事实:如果在
buildingQuery事件期间添加了->orderBy()子句,这将导致忽略$model->orderBy属性。
查询构建器事件调用方式与模型事件类似,只是通过 buildingQuery 静态方法传递查询对象。
仅向访客用户显示已发布内容的示例
News::buildingQuery(function($query) {
if (!Auth::check()) {
return $query->where('published', 1);
}
return $query;
});
该方法必须返回 FALSE、$query 或 Illuminate\Database\Query\Builder 的一个实例。
注意:此功能目前不适用于 $query->lists() 方法
在测试试验中已记录
Eloquental 已完全进行单元测试。测试位于 Eloquental 包的 tests 目录中,可以通过在包的基础目录中运行 phpunit 来执行这些测试。
授权给所有尊贵的人士
Eloquental 采用 GPLv2 许可