2017 © Pedro Peláez
 

library yetorm

Lightweight ORM for Nette\Database

image

uestla/yetorm

Lightweight ORM for Nette\Database

  • Wednesday, February 21, 2018
  • by uestla
  • Repository
  • 6 Watchers
  • 49 Stars
  • 8,415 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 11 Forks
  • 1 Open issues
  • 45 Versions
  • 16 % Grown

The README.md

YetORM

Build Status Code coverage Total downloads Latest stable, (*1)

Lightweight ORM built on top of Nette\Database, (*2)

Buy me a Coffee, (*3)

Quickstart

Consider following database schema:, (*4)

Database schema, (*5)

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., (*6)

Tag

/**
 * @property-read int $id
 * @property string $name
 */
class Tag extends YetORM\Entity
{}

Author

/**
 * @property-read int $id
 * @property string $name
 * @property string $web
 * @property \DateTime $born
 */
class Author extends YetORM\Entity
{}

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., (*7)

/**
 * @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');
    }
}

With $record->ref($table, $column) we're accessing related table row in table $table through column $column - pretty simple., (*8)

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 :-), (*9)

This sounds crazy, but it's actually simple to get used to., (*10)

With this knowledge we can now simply add some helpful methods to Author entity:, (*11)

// 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');
}

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., (*12)

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

Fetching collections

YetORM\Repository comes with basic findAll() and findBy($criteria) methods, both returning already mentioned EntityCollection., (*13)

We can simply iterate through all books, (*14)

$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;
    }
}

Fetching single entity

$book = $books->getByID(123); // instanceof Book or NULL if not found

Magic findBy<Property>() and getBy<Property>() methods

Instead of manually writing findByTitle($title) method as this, (*15)

function findByTitle($title)
{
    return $this->findBy(array(
        'title' => $title,
    ));
}

we can just call, (*16)

$books->findByTitle($title); // without having the method implemented

That will return a collection of books with that exact title., (*17)

To get a single entity use the magic getBy<Property>($value) method:, (*18)

$book = $books->getByIsbn('<isbn_code>'); // instanceof Book or NULL if not found

Just to have the IDE code completion along with this magic methods, we can use the @method annotation:, (*19)

/**
 * @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
 {}

IMPORTANT: When using magic findBy<Property>() and getBy<Property>() methods, make sure you have the property defined via @property annotation!, (*20)

NOTE: magic findBy<Property>() and getBy<Property>() do not work on relational properties of type Entity., (*21)

Persisting

To persist changes we simply call $repository->persist($entity)., (*22)

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

And that's it!, (*23)

Additional notes

  • No identity map
  • Query efficiency - the collections (resp. YetORM\Record) use the power of Nette\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., (*24)

The Versions

21/02 2018

dev-master

9999999-dev https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

21/02 2018
26/03 2017
24/03 2017
08/12 2016
19/07 2016
13/07 2016

10.0.0

10.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

12/07 2016

9.0.3

9.0.3.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

12/07 2016

9.0.2

9.0.2.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

15/03 2016

9.0.1

9.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

04/03 2016

9.0.0

9.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

03/03 2016

8.0.7

8.0.7.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

08/09 2015

8.0.6

8.0.6.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

31/05 2015

8.0.5

8.0.5.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

24/02 2015

8.0.4

8.0.4.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

07/01 2015

8.0.3

8.0.3.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

06/01 2015

8.0.2

8.0.2.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

08/12 2014

8.0.1

8.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

10/09 2014

8.0.0

8.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

25/08 2014

7.0.1

7.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

16/08 2014

7.0.0

7.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

08/08 2014

6.0.3

6.0.3.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

06/08 2014

6.0.2

6.0.2.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

04/08 2014

6.0.1

6.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

28/05 2014

6.0.0

6.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

25/05 2014
22/05 2014
22/05 2014

4.0.0

4.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

19/05 2014

3.0.1

3.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

19/05 2014

3.0.0

3.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

05/04 2014

3.0.0b

3.0.0.0-beta https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

19/01 2014

2.1.0

2.1.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

07/01 2014

2.0.1

2.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

05/01 2014

2.0.0

2.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

01/12 2013

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

01/12 2013

1.2.4

1.2.4.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

23/08 2013

1.2.3

1.2.3.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

03/08 2013

1.2.2

1.2.2.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

26/07 2013

1.2.1

1.2.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

25/07 2013

1.2.0

1.2.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

18/07 2013

1.1.0

1.1.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

18/07 2013

1.0.2

1.0.2.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

11/07 2013

1.0.1

1.0.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

12/05 2013

1.0.0

1.0.0.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette

07/04 2013

0.9.1

0.9.1.0 https://github.com/uestla/YetORM

Lightweight ORM for Nette\Database

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database nette