kocicak/yetorm

轻量级ORM,用于Nette\Database

11.0.1 2019-05-02 09:20 UTC

README

Build Status Code coverage Total downloads Latest stable

基于Nette\Database的轻量级ORM

Buy me a Coffee

快速入门

考虑以下数据库模式

Database schema

实体

首先,我们将根据上面的模式创建实体类。定义实体属性有两种方式 - 通过@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)进行限制

更多

有关更多示例,请参阅测试