Download the PHP package crocodile2u/tinyorm without Composer
On this page you can find all versions of the php package crocodile2u/tinyorm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package tinyorm
tinyorm
Very minimalistic ORM & DB tools for PHP
Why yet another library?
I know quite a lot of similar projects but they all don't satisfy me. Therefore, I made a list of requirements that my perfect ORM library should meet.
- It should be tiny. I don't want to have tons of classes added to my next project just because I want to automate database routines.
- It should not be too smart. It should help me to perform ordinary tasks and stay as much under control as possible.
- It should not generate DB schema. For migrations I will use special instruments.
- No lazy-loading of related objects. No this magic, no behind-the-scene bullshit.
- No weird aliases in SQL like SELECT * FROM \model\Foo JOIN \model\Bar ...
- Thin entities that do not contain any buisiness logic or even the storage-specific logic (relations etc). This makes things overcomplicated and results in less control from the developer. Separation of persistence logic from the entities makes them reusable even when you switch storage backends. While this is not often needed, this is a handy feature which is easily achievable. Moreover, sometimes I would like, for example, to access the same entity in different ways: for example, if I have a table which I want to access via both MySQL & Handlersocket.
- A Query-Object implementation with a neat interface. I usually prefer to write SQL queries by hand. However, there are cases when the query needs to be formed dynamically, and in such cases an object with clean interface is a great advantage over composing SQL string by hand.
- It should be able of handling multiple DB connections. Ideally it should have a transaction manager which issues BEGIN/COMMIT/ROLLBACK for all connections participating in DB interactions.
- Ideally it should have a scaffolding utility to generate entities from DB tables.
While there are tools that conform to some of the requirements, I failed to find a library that has it all.
Show me the code!
Select usage:
You can set fetch modes for Select:
Working with multiple DB connections:
This way, if anything goes wrong with the first or the second INSERT, transactions in both connection will be rolled back, no rows will be inserted. On the other hand, if everything goes fine, transactions in both connection will be commited.
Transaction manager supports nested transactions, and the tinyorm\Db class also supports them.
The approach
I used an approach similar to that of Zend Framework 2 ( http://framework.zend.com/manual/current/en/user-guide/database-and-models.html ). The entity classes are just simple data containers that do not have DB connection/persistence logic. However, in the end, tinyorm entities do have minimal "knowledge" about their relationship to a storage layer. First, they have getSourceName() method which essentially is meant to return a storage table/collection name. Second, there are getPK() and setPK() methods to access primary key. The name primary key column is stored in protected pkName property. And finally, entities have getSequenceName(). I made all this for the sake of simplicity, in order not to have to introduce more classes. tinyorm only supports _AUTOINCREMENT'ed primary keys. In contracts to Zend Framework 2, all the stuff related to persistence is just a few classes/interfaces:
- DbInterface - the interface for Db connector
- Db - a wrapper around PDO
- persistence\Driver - the interface for persistence driver
- persistence\DbDriver - persistence driver implementation with Db/PDO (thus RDBMS) as backend. Only tested with MySQL, though should also work fine with Postgres and Sqlite
- persistence\HsDriver - persistence driver implementation with handlersocket as backend.
Persistence driver operates on Entities. In case of ZF2, we can talk about their Table Gateway as a persistence driver. See the link above for reference. In tinyorm, things are way more simple. You just create a persistence driver instance and call its' save(), insert(), update(), delete() methods providing an Entity as an argument.
For a query object, I took a look at Phalcon framework ( https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html ). However, I modified the interface a little, so I find it a little bit better.
I also implemented a database transaction manager capable of handling multiple DB connections.
Credits
Thanks RasmiKanta Moharana (https://github.com/rashmi8105) for early feedback & spotting bugs in the example app!
All versions of tinyorm with dependencies
ext-pdo Version *