vakata / orm
An ORM implementation
7.3.0
2018-05-25 11:23 UTC
Requires
- php: >=7.0.0
- vakata/database: ^3.7
Requires (Dev)
- clean/phpdoc-md: dev-master
- phpunit/phpunit: 4.*
- dev-master
- 7.3.0
- 7.2.1
- 7.2.0
- 7.1.3
- 7.1.2
- 7.1.1
- 7.1.0
- 7.0.0
- 6.2.5
- 6.2.4
- 6.2.3
- 6.2.2
- 6.2.1
- 6.2.0
- 6.1.0
- 6.0.0
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.1
- 5.0.0
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-11-10 05:45:45 UTC
README
A orm abstraction with support for various drivers.
Install
Via Composer
$ composer require vakata/orm
Usage
// first you need a database instance $db = new \vakata\database\DB('mysql://root@127.0.0.1/test'); // then you can create the manager object $manager = new \vakata\orm\Manager($db); // assuming there is a book table with a name column foreach ($manager->book() as $book) { echo $book->name . "\n"; } // you could of course filter and order foreach ($manager->book()->filter('year', 2016)->sort('name') as $book) { // iterate over the books from 2016 } // if using mySQL foreign keys are automatically detected // for example if there is an author table and the book table references it foreach ($manager->book() as $book) { echo $book->author->name . "\n"; } // you can solve the n+1 queries problem like this foreach ($manager->fromQuery($db->book()->with('author')) as $book) { echo $book->author->name . "\n"; } // provided there is a linking table book_tag and a tag table and each book has many tags you can do this foreach ($manager->book()->with('author') as $book) { echo $book->tag[0]->name . "\n"; // the name of the first tag which the current book has } // which means you can do something like this echo $manager->book()[0]->author->book[0]->tag[0]->book[0]->author->name; // as for removing objects $authors = $manager->author(); $author = $authors[0]; $authors->remove($author); // you can also create new objects $book = new \StdClass(); $book->name = 'Test'; $manager->books()->add($book); // you can also use your own classes // just make sure you provide getters and setters for all table columns and relations class Author { protected $data = []; public function __get($key) { return $this->data[$key] ?? null; } public function __set($key, $value) { $this->data[$key] = $value; } } $manager->registerGenericMapperWithClassName('author', Author::class); // you can also add custom mappers (check the docs) $author = $manager->author()[0]; // this is an Author instance // as for changing objects // one way is to do it manually $authors = $manager->author(); $author = $authors[0]; $author->name = "Test"; $manager->getMapper('author')->update($author); // or use the unit of work manager and have all changes automatically saved $manager = new \vakata\orm\UnitOfWorkManager($db, new \vakata\orm\UnitOfWork($db)); $book = new Book(); $book->name = "Book name"; $book->author = $manager->author()->find(1); $tag = new Tag('tag'); $book->tags = $tag; $book->author->name = "Changed name"; $manager->books()->add($book); $manager->tags()->add($tag); // all you need to do is call this line $manager->save();
Read more in the API docs
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email github@vakata.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.