kocicak / yetorm
轻量级ORM,用于Nette\Database
Requires
- php: >=7.1
- nette/database: ^3.0
- nette/reflection: ^2.4
- nette/utils: ^3.0
Requires (Dev)
- nette/caching: ^3.0
- nette/robot-loader: ^3.0
- nette/tester: ^2.0
- tracy/tracy: ^2.6
This package is auto-updated.
Last update: 2024-09-14 16:18:57 UTC
README
基于Nette\Database的轻量级ORM
快速入门
考虑以下数据库模式
实体
首先,我们将根据上面的模式创建实体类。定义实体属性有两种方式 - 通过@property[-read]
注解,或者简单地通过getter和setter。
标签
/** * @property-read int $id * @property string $name */ class Tag extends YetORM\Entity {}
作者
/** * @property-read int $id * @property string $name * @property string $web * @property \DateTime $born */ class Author extends YetORM\Entity {}
书籍
在Book
实体中存在一些关系 - 两个N:1 Author
和M:N Tag
关系。每个YetORM\Entity
都包含一个YetORM\Record
实例,它是Nette\Database\Table\ActiveRow
的简单包装。这意味着我们可以通过它访问相关记录或列值。
/** * @property-read int $id * @property string $title * @property string $web * @property string $slogan */ class Book extends YetORM\Entity { function getAuthor() { return new Author($this->record->ref('author', 'author_id')); } function getMaintainer() { return new Author($this->record->ref('author', 'maintainer_id')); } function getTags() { $selection = $this->record->related('book_tag'); return new YetORM\EntityCollection($selection, 'Tag', 'tag'); } }
使用$record->ref($table, $column)
,我们通过列$column
访问$table
表中的相关行 - 非常简单。
M:N关系通过YetORM\EntityCollection
实例实现 - 这是一个实体惰性集合。在这种情况下,它迭代所有从book_tag
表(第一个参数)相关的行,创建Tag
(第二个参数)的实例,并在每个相关的book_tag
表行中访问相关的tag
表行(第三个参数),然后将其传递给Tag
实体构造函数 :-)
这听起来很疯狂,但实际上很容易习惯。
有了这些知识,我们现在可以简单地向Author
实体添加一些有用的方法
// class Author function getBooksWritten() { $selection = $this->record->related('book', 'author_id'); return new YetORM\EntityCollection($selection, 'Book'); } function getBooksMaintained() { $selection = $this->record->related('book', 'maintainer_id'); return new YetORM\EntityCollection($selection, 'Book'); }
仓库
每个仓库都必须定义表和实体类名 - 通过@table
和@entity
注解,或者通过受保护的$table
和$entity
类属性。
/** * @table book * @entity Book */ class BookRepository extends YetORM\Repository {}
获取集合
YetORM\Repository
提供了基本的findAll()
和findBy($criteria)
方法,两者都返回前面提到的EntityCollection
。
我们可以简单地遍历所有书籍
$books = new BookRepository($connection); // $connection instanceof Nette\Database\Context foreach ($books->findAll() as $book) { // $book instanceof Book echo $book->title; echo $book->getAuthor()->name; foreach ($book->getTags() as $tag) { // $tag instanceof Tag echo $tag->name; } }
获取单个实体
$book = $books->getByID(123); // instanceof Book or NULL if not found
魔法findBy<Property>()
和getBy<Property>()
方法
而不是手动编写findByTitle($title)
方法如下
function findByTitle($title) { return $this->findBy(array( 'title' => $title, )); }
我们只需调用
$books->findByTitle($title); // without having the method implemented
这将返回具有该确切标题的书籍集合。
要获取单个实体,请使用魔法getBy<Property>($value)
方法
$book = $books->getByIsbn('<isbn_code>'); // instanceof Book or NULL if not found
为了使IDE代码补全与这些魔法方法一起使用,我们可以使用@method
注解
/** * @table book * @entity Book * @method YetORM\EntityCollection|Book[] findByTitle(string $title) * @method Book|NULL getByIsbn(string $isbn) */ class BookRepository extends YetORM\Repository {} /** * @property-read int $id * @property string $title * @property string $isbn */ class Book extends Entity {}
重要提示:当使用魔法
findBy<Property>()
和getBy<Property>()
方法时,请确保您已经通过@property
注解定义了属性!
注意:魔法
findBy<Property>()
和getBy<Property>()
不适用于类型为Entity的关联属性。
持久化
要持久化更改,我们只需调用$repository->persist($entity)
。
$book->web = 'http://example.com'; $books->persist($book);
就是这样!
其他注意事项
- 无身份映射
- 查询效率 - 集合(或
YetORM\Record
)使用Nette\Database
的效率 - 集合操作 - 集合可以通过
$coll->orderBy($column, $dir)
进行排序,并通过$coll->limit($limit, $offset)
进行限制
更多
有关更多示例,请参阅测试。