简单来说,存储库允许您的所有代码使用对象,而无需知道对象是如何持久化的。存储库包含持久性的所有知识,包括从表到对象的映射。这提供了持久层的更加面向对象的视图,并使映射代码更加封装。
让你的存储库在 laravel 中工作(作为一个真正的存储库——eric evans 领域驱动设计书)的唯一方法是将默认的 orm 从活动记录更改为数据映射器。最好的替代品是教义。
学说 ormdoctrine 是一种 orm(对象关系映射),它实现了数据映射器模式,并允许您将应用程序的业务规则与数据库的持久层完全分离。 doctrine 使用 dql,而不是 sql。 dql 为您带来对象查询语言,这意味着您将使用对象术语进行查询,而不是传统的关系查询术语。
它允许您以面向对象的方式编写数据库查询,并在您需要以使用默认存储库方法无法实现的方式查询数据库时提供帮助。在我看来,dql 是与数据库保持联系的最强大方式。
教条与雄辩doctrine 实体只是一个普通的 php 简单类,不会增加任何 orm 继承的开销。 doctrine 使用相同的继承来管理多个查询请求,而无需访问数据库,这意味着整个请求都存在实体对象。
doctrine 的另一个不错的功能是,无需迁移文件来创建数据库模式,而是自动创建数据库来反映实体注释中的元数据。另一方面,eloquent 不太复杂并且非常易于使用。
这两者之间的完整比较需要一篇单独的文章。正如您所看到的,doctrine 对象更轻、更抽象。然而,doctrine 只适合特定的项目,因此它有时可能会给您带来开销。我相信这取决于程序员为应用程序选择最好的 orm。
博客应用现在是时候使用 laravel 创建一个博客应用程序了。首先,我们需要建立教义。有一个桥梁可以与 laravel 5 的现有配置进行匹配。要在 laravel 项目中安装 doctrine 2,我们运行以下命令:
composer require laravel-doctrine/orm
像往常一样,该包应该添加到app/config.php ,作为服务提供者:
laraveldoctrine\orm\doctrineserviceprovider::class,
还应该配置别名:
'entitymanager' => laraveldoctrine\orm\facades\entitymanager::class
最后,我们发布包配置:
php artisan vendor:publish --tag=config
现在我们已经完成了。
实体是应用程序 app\entities\post.php 的重要组成部分:
namespace app\entity;use doctrine\orm\mapping as orm;/** * @orm\entity * @orm\table(name=posts) * @orm\haslifecyclecallbacks() */class post{ /** * @var integer $id * @orm\column(name=id, type=integer, unique=true, nullable=false) * @orm\id * @orm\generatedvalue(strategy=auto) * */ private $id; /** * @orm\column(type=string) */ private $title; /** * @orm\column(type=text) */ private $body; public function __construct($input) { $this->settitle($input['title']); $this->setbody($input['body']); } public function setid($id) { return $this->id=$id; } public function getid() { return $this->id; } public function gettitle() { return $this->title; } public function settitle($title) { $this->title = $title; } public function getbody() { return $this->body; } public function setbody($body) { $this->body = $body; }}
现在是时候创建存储库了,这在前面已经描述过了。 app/repositories/postrepo.php :
namespace app\repository;use app\entity\post;use doctrine\orm\entitymanager;class postrepo{ /** * @var string */ private $class = 'app\entity\post'; /** * @var entitymanager */ private $em; public function __construct(entitymanager $em) { $this->em = $em; } public function create(post $post) { $this->em->persist($post); $this->em->flush(); } public function update(post $post, $data) { $post->settitle($data['title']); $post->setbody($data['body']); $this->em->persist($post); $this->em->flush(); } public function postofid($id) { return $this->em->getrepository($this->class)->findoneby([ 'id' => $id ]); } public function delete(post $post) { $this->em->remove($post); $this->em->flush(); } /** * create post * @return post */ private function perparedata($data) { return new post($data); }}
控制器:app/http/controllers/postcontroller.php :
namespace app\http\controllers;use app\repository\postrepo as repo;use app\validation\postvalidator;class postcontroller extends controller{ private $repo; public function __construct(repo $repo) { $this->repo = $repo; } public function edit($id=null) { return view('admin.edit')->with(['data' => $this->repo->postofid($id)]); } public function editpost() { $all = input::all(); $validate = postvalidator::validate($all); if (!$validate->passes()) { return redirect()->back()->withinput()->witherrors($validate); } $id = $this->repo->postofid($all['id']); if (!is_null($id)) { $this->repo->update($id, $all); session::flash('msg', 'edit success'); } else { $this->repo->create($this->repo->perpare_data($all)); session::flash('msg', 'add success'); } return redirect()->back(); } public function retrieve() { return view('admin.index')->with(['data' => $this->repo->retrieve()]); } public function delete() { $id = input::get('id'); $data = $this->repo->postofid($id); if (!is_null($data)) { $this->repo->delete($data); session::flash('msg', 'operation success'); return redirect()->back(); } else { return redirect()->back()->witherrors('operationfails'); } }}
如您所见,我使用 flash 助手来管理消息(您可以使用 laravel 的)。关于验证器,我应该补充一点,您可以创建自己的验证器(就像我一样)或使用 laravel 默认值,具体取决于您的偏好。
查看文件与平常相同。在此示例视图中,文件看起来像 resources/views/admin/edit.blade.php :
@if (session::has('flash_notification.message')) × {!! session::get('flash_notification.message') !!} @endif @if($errors->has()) @foreach ($errors->all() as $error) {!! $error !!} @endforeach @endif {!! 'title' !!} {!! 'body' !!} {!! is_object($listdata)?$listdata->gettitle():'' !!} {!! 'save' !!}
以上就是laravel 5中的仓储模式的详细内容。
