hranicka / yetorm
Requires
- php: >=5.4.0
- nette/caching: ~2.2
- nette/database: ~2.3
- nette/di: ~2.2
- nette/reflection: ~2.2
- nette/robot-loader: ~2.2
- nette/utils: ~2.2
Requires (Dev)
- phpunit/phpunit: ~4.5
- dev-master
- 3.14.0
- 3.13.0
- 3.12.1
- 3.12.0
- 3.11.0
- 3.10.2
- 3.10.1
- 3.10.0
- 3.9.0
- 3.8.0
- 3.7.5
- 3.7.4
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0b
- 2.1.0
- 2.0.1
- 2.0.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.9.1
This package is auto-updated.
Last update: 2019-02-20 17:48:19 UTC
README
此包已被放弃且不再维护。作者建议使用 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
快速入门
考虑以下数据库模式
安装
设置 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)
限制
更多
有关更多示例,请参阅 测试。