Download the PHP package uestla/yetorm without Composer
On this page you can find all versions of the php package uestla/yetorm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download uestla/yetorm
More information about uestla/yetorm
Files in uestla/yetorm
Package yetorm
Short Description Lightweight ORM for Nette\Database
License MIT
Homepage https://github.com/uestla/YetORM
Informations about the package yetorm
YetORM
Lightweight ORM built on top of Nette\Database
Quickstart
Consider following database schema:
Entities
Firstly we'll create entity classes according to the schema above. There are two ways of defining entity properties - via @property[-read]
annotation, or simply via getter and setter.
Tag
Author
Book
There are some relations at the Book
entity - two N:1 Author
and M:N Tag
relations. Every YetORM\Entity
has an instance of YetORM\Record
in it, which is a simple wrapper around Nette\Database\Table\ActiveRow
. That means that we can access related records or column values through it.
With $record->ref($table, $column)
we're accessing related table row in table $table
through column $column
- pretty simple.
The M:N relation is realized with YetORM\EntityCollection
instance - which is a lazy collection of entities. In this case it iterates throw all related rows from book_tag
table (first argument), creates instances of Tag
(second argument) and on every related book_tag
table row it accesses related tag
table row (third argument), which then passes to the constructor of Tag
entity :-)
This sounds crazy, but it's actually simple to get used to.
With this knowledge we can now simply add some helpful methods to Author
entity:
Repositories
Every repository has to have table and entity class name defined - either via @table
and @entity
annotaion, or via protected $table
and $entity
class property.
Fetching collections
YetORM\Repository
comes with basic findAll()
and findBy($criteria)
methods, both returning already mentioned EntityCollection
.
We can simply iterate through all books
Fetching single entity
Magic findBy<Property>()
and getBy<Property>()
methods
Instead of manually writing findByTitle($title)
method as this
we can just call
That will return a collection of books with that exact title.
To get a single entity use the magic getBy<Property>($value)
method:
Just to have the IDE code completion along with this magic methods, we can use the @method
annotation:
IMPORTANT: When using magic
findBy<Property>()
andgetBy<Property>()
methods, make sure you have the property defined via@property
annotation!NOTE: magic
findBy<Property>()
andgetBy<Property>()
do not work on relational properties of type Entity.
Persisting
To persist changes we simply call $repository->persist($entity)
.
And that's it!
Additional notes
- No identity map
- Query efficiency - the collections (resp.
YetORM\Record
) use the power ofNette\Database
efficiency - Collection operations - collections can be sorted via
$coll->orderBy($column, $dir)
and limitted via$coll->limit($limit, $offset)
More
For more examples please see the tests.
All versions of yetorm with dependencies
nette/utils Version ^2.4
nette/database Version ^2.4
nette/reflection Version ^2.3