hranicka/yetorm

此包已被放弃且不再维护。作者建议使用 artfocus/jetorm 包。

轻量级 ORM for Nette\Database

3.14.0 2015-09-15 07:49 UTC

README

Build Status

此包已被放弃且不再维护。作者建议使用 artfocus/jetorm 包。

THIS IS NOT OFFICIAL YetORM LIBRARY. IT'S A MODIFIED FORK.
USE RATHER OFFICIAL ONE:
https://github.com/uestla/YetORM

YetORM

基于 Nette\Database 构建的轻量级 ORM

快速入门

考虑以下数据库模式

Database schema

安装

设置 config.neon 如下

extensions:
	yetorm: YetORM\Extension

yetorm:
	# setup cache IStorage for better performance
	# setup this only on production otherwise Entity doesn't load new Refletion until cache will be deleted!
	storage: cacheStorage # for development, leave this value empty (without "cacheStorage")

实体

首先,我们将根据上述模式创建实体类。定义实体属性有两种方式 - 通过 @property[-read] 注解,或者简单地通过 getter 和 setter。

标签

/**
 * @property-read int $id
 * @property string $name
 *
 * @method int getId()
 * @method string getName()
 *
 * @method Tag setName(string $name)
 */
class Tag extends YetORM\Entity
{
	
}

作者

/**
 * @property-read int $id
 * @property string $name
 * @property string $web
 * @property \DateTime $born
 *
 * @method int getId()
 * @method string getName()
 * @method string getWeb()
 * @method \DateTime getBorn()
 *
 * @method Author setName(string $name)
 * @method Author setWeb(string $web)
 * @method Author setBorn(\DateTime $born)
 */
class Author extends YetORM\Entity
{
	
}

书籍

Book 实体中存在一些关系 - 两个 N:1 的 Author 和 M:N 的 Tag 关系。每个 YetORM\Entity 中都有一个 YetORM\Row 的实例,它是 Nette\Database\Table\ActiveRow 的简单包装。这意味着我们可以通过它访问相关行或列值。

/**
 * @property-read int $id
 * @property string $title
 * @property string $web
 * @property string $slogan
 * @property-read Author $author
 *
 * @method int getId()
 * @method string getTitle()
 * @method string getWeb()
 * @method string getSlogan()
 *
 * @method Book setTitle(string $title)
 * @method Book setWeb(string $web)
 * @method Book setSlogan(string $slogan)
 */
class Book extends YetORM\Entity
{

	public function getAuthor()
	{
		return $this->getOne(Author::getClassName(), 'author', 'author_id', TRUE);
	}

	public function getMaintainer()
	{
		return $this->getOne(Author::getClassName(), 'author', 'maintainer_id', TRUE);
	}

	public function getTags()
	{
		return $this->getMany(Tag::getClassName(), 'book_tag', 'tag');
	}
	
}

使用 $row->ref($table, $column) 我们通过列 $column 访问表 $table 中的相关行 - 非常简单。

M:N 关系通过 YetORM\EntityCollection 实例实现 - 它是实体懒加载集合。在这种情况下,它遍历来自 book_tag 表的所有相关行(第一个参数),创建 Tag 的实例(第二个参数),并在每个相关的 book_tag 表行中访问相关的 tag 表行(第三个参数),然后将其传递给 Tag 实体的构造函数 :-)

这听起来很疯狂,但实际上很容易习惯。

有了这些知识,我们现在可以简单地向 Author 实体添加一些有用的方法

// class Author
public function getBooksWritten()
{
	return $this->getMany(Book::getClassName(), 'book', 'book', 'author_id');
}

public function getBooksMaintained()
{
	return $this->getMany(Book::getClassName(), 'book', 'book', 'maintainer_id');
}

仓库

每个仓库都必须定义表和实体类名称 - 通过 @table@entity 注解,或者通过受保护的 $table$entity 类属性。

/**
 * @table book
 * @entity Book
 */
class BookRepository extends YetORM\Repository
{
	
}

持久化

要持久化更改,只需调用 $repository->persist($entity)

$book->setWeb('http://example.com');
$books->persist($book);

就这么多!

附加说明

  • 无身份映射
  • 查询效率 - 集合(或 YetORM\Row)使用 Nette\Database 的效率
  • 集合操作 - 集合可以通过 $coll->orderBy($column, $dir) 排序,并通过 $coll->limit($limit, $offset) 限制

更多

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