artfocus/jetorm

此包已被 废弃 且不再维护。作者建议使用 doctrine/orm 包。

基于 uestla\yetorm 的 Nette\Database ORM

v3.6.2 2016-10-06 10:47 UTC

README

ATTENTION:

THIS PACKAGE IS NOT A THING YOU WANT TO WORK WITH.

PLEASE DO NOT USE IT IF YOU HAVE AN OPTION.

USE DOCTRINE 2 INSTEAD. REALLY. IT SAVES YOUR NERVES.

Artfocus\JetORM

基于 Nette\Database 构建的轻量级 ORM。基于 uestla/yetormhranicka/yetorm。谢谢!

快速入门

考虑以下数据库模式

Database schema

安装

设置 config.neon 如下

extensions:
	jetorm: Artfocus\JetORM\Extension

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

实体

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

标签

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

作者

/**
 * @property-read int|null $id
 * @property string $name
 * @property string $web
 * @property \DateTime $born
 *
 * @method int|null 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 Artfocus\JetORM\Entity
{
	
}

书籍

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

/**
 * @property-read int|null $id
 * @property string $title
 * @property string $web
 * @property string $slogan
 * @property Author $author -> :one(author)
 * @property Author $maintainer -> :one(author, maintainer_id)
 * @property-read Tag[]|Artfocus\JetORM\Collection $tags -> :many(book_tag, tag)
 *
 * @method int|null getId()
 * @method string getTitle()
 * @method string getWeb()
 * @method string getSlogan()
 * @method Author getAuthor()
 * @method Author getMaintainer()
 * @method Tag[]|Artfocus\JetORM\Collection getTags()
 *
 * @method Book setTitle(string $title)
 * @method Book setWeb(string $web)
 * @method Book setSlogan(string $slogan)
 * @method Book setAuthor(Author $author)
 * @method Book setMaintainer(Author $maintainer)
 */
class Book extends Artfocus\JetORM\Entity
{

}

M:N 关系通过 Artfocus\JetORM\Collection 实例实现,默认为 Artfocus\JetORM\EntityCollection - 这是一个延迟加载的实体集合。

存储库

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

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

持久化

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

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

就这些了!

附加说明

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

更多

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