PHP Classes
elePHPant
Icontem

Tiny PHP ORM Framework: Map objects to databases using composed queries

Recommend this page to a friend!
  Info   View files Example   View files View files (83)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2018-03-12 (6 months ago) RSS 2.0 feedStarStarStarStarStar 81%Total: 826 This week: 1All time: 4,099 This week: 468Up
Version License PHP version Categories
tinyorm 1.15BSD License5.4PHP 5, Databases, Libraries, Design P...
Collaborate with this project Author

tinyorm - github.com

Description

This package can map objects to databases using composed queries.

It provides base entity classes that should be extended implementation classes that define how to map objects to tables, primary key fields, what fields to retrieve to map object variables, etc..

The package also provide means to access the database abstraction layer and execute SQL queries to store and retrieve the entity class object as well manage transactions.

Object queries can be composed using a fluent interface to define several parameters like the classes of the objects to map, conditions, join clauses, etc..

The database access uses PDO classes.

  Performance   Level  
Name: Victor Bolshov <contact>
Classes: 10 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 2x

Details

tinyorm

Very minimalistic ORM &amp; 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 &amp; 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:

$select = (new Select("my_table"))
    ->join("LEFT JOIN another_table USING (join_column)")
    ->where("filter_column = ?", $filterColumnValue)
    ->setConnection($db);
$count = $select->count("DISTINCT id");
$rows = $select->execute()->fetchAll(\PDO::FETCH_ASSOC);

You can set fetch modes for Select:

$select->setFetchMode(\PDO::FETCH_ASSOC);
$select->setFetchClass(MyTableEntity::class);
$select->setFetchInto(new MyTableEntity());

Working with multiple DB connections:

$txManager = new \tinyorm\TxManager();
$txManager->registerConnection($this->connection)
    ->registerConnection($this->connection2);

$result = $txManager->atomic(function () {
    $this->connection->exec("INSERT INTO test (c_unique) VALUES ('val1')");
    $this->connection2->exec("INSERT INTO test (c_unique) VALUES ('val2')");
    return true;
});

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 _AUTO_INCREMENT_'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!

  Files folder image Files  
File Role Description
Files folder imagebin (1 file)
Files folder imageexamples (1 directory)
Files folder imagelib (8 files, 4 directories)
Files folder imagetest (5 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  bin  
File Role Description
  Accessible without login Plain text file tinyorm Example Example script

  Files folder image Files  /  examples  
File Role Description
Files folder imagelibrary (1 file, 5 directories)

  Files folder image Files  /  examples  /  library  
File Role Description
Files folder imageetc (1 file)
Files folder imagelib (5 files, 1 directory)
Files folder imagevar (1 file)
Files folder imageview (8 files, 1 directory)
Files folder imagewww (16 files, 1 directory)
  Accessible without login Plain text file bootstrap.php Appl. Application script

  Files folder image Files  /  examples  /  library  /  etc  
File Role Description
  Accessible without login Plain text file config.base.php Conf. Configuration script

  Files folder image Files  /  examples  /  library  /  lib  
File Role Description
Files folder imagescaffold (4 files)
  Plain text file Author.php Class Class source
  Accessible without login Plain text file autoload.inc Aux. Auxiliary script
  Plain text file Book.php Class Class source
  Plain text file Registry.php Class Class source
  Plain text file View.php Class Class source

  Files folder image Files  /  examples  /  library  /  lib  /  scaffold  
File Role Description
  Plain text file Author.php Class Class source
  Plain text file Book.php Class Class source
  Plain text file BookHasAuthor.php Class Class source
  Plain text file Edition.php Class Class source

  Files folder image Files  /  examples  /  library  /  var  
File Role Description
  Accessible without login Plain text file library.sql Data Auxiliary data

  Files folder image Files  /  examples  /  library  /  view  
File Role Description
Files folder imagesidebar (6 files)
  Accessible without login Plain text file authors.php Aux. Auxiliary script
  Accessible without login Plain text file author_edit.php Example Example script
  Accessible without login Plain text file books.php Aux. Auxiliary script
  Accessible without login Plain text file book_edit.php Aux. Auxiliary script
  Accessible without login Plain text file edition_edit.php Aux. Auxiliary script
  Accessible without login Plain text file footer.php Aux. Auxiliary script
  Accessible without login Plain text file header.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  examples  /  library  /  view  /  sidebar  
File Role Description
  Accessible without login HTML file authors.html Doc. Documentation
  Accessible without login HTML file author_edit.html Doc. Documentation
  Accessible without login HTML file books.html Doc. Documentation
  Accessible without login HTML file book_edit.html Doc. Documentation
  Accessible without login HTML file edition_edit.html Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation

  Files folder image Files  /  examples  /  library  /  www  
File Role Description
Files folder imagebootstrap (3 directories)
  Accessible without login Plain text file authors.php Example Application script
  Accessible without login Plain text file author_delete.php Example Example script
  Accessible without login Plain text file author_edit.php Example Example script
  Accessible without login Plain text file author_save.php Example Example script
  Accessible without login Plain text file books.php Example Example script
  Accessible without login Plain text file book_add.php Example Example script
  Accessible without login Plain text file book_author_add.php Example Example script
  Accessible without login Plain text file book_author_delete.php Example Example script
  Accessible without login Plain text file book_delete.php Example Example script
  Accessible without login Plain text file book_edit.php Example Example script
  Accessible without login Plain text file book_save.php Example Example script
  Accessible without login Plain text file edition_delete.php Example Example script
  Accessible without login Plain text file edition_edit.php Example Example script
  Accessible without login Plain text file edition_save.php Example Example script
  Accessible without login Plain text file index.php Example Example script
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files  /  examples  /  library  /  www  /  bootstrap  
File Role Description
Files folder imagecss (8 files)
Files folder imagefonts (1 file)
Files folder imagejs (3 files)

  Files folder image Files  /  examples  /  library  /  www  /  bootstrap  /  css  
File Role Description
  Accessible without login Plain text file bootstrap-theme.css Data Auxiliary data
  Accessible without login Plain text file bootstrap-theme.css.map Data Auxiliary data
  Accessible without login Plain text file bootstrap-theme.min.css Data Auxiliary data
  Accessible without login Plain text file bootstrap-theme.min.css.map Data Auxiliary data
  Accessible without login Plain text file bootstrap.css Data Auxiliary data
  Accessible without login Plain text file bootstrap.css.map Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.css.map Data Auxiliary data

  Files folder image Files  /  examples  /  library  /  www  /  bootstrap  /  fonts  
File Role Description
  Accessible without login Plain text file glyphicons-halflings-regular.svg Data Auxiliary data

  Files folder image Files  /  examples  /  library  /  www  /  bootstrap  /  js  
File Role Description
  Accessible without login Plain text file bootstrap.js Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.js Data Auxiliary data
  Accessible without login Plain text file npm.js Data Auxiliary data

  Files folder image Files  /  lib  
File Role Description
Files folder imagelog (2 files)
Files folder imagepersistence (3 files)
Files folder imagescaffold (1 file)
Files folder imagetest (3 files)
  Plain text file Bind.php Class Class source
  Plain text file Db.php Class Class source
  Plain text file DbInterface.php Class Class source
  Plain text file Entity.php Class Class source
  Plain text file EntityCollection.php Class Class source
  Plain text file Select.php Class Class source
  Plain text file Statement.php Class Class source
  Plain text file TxManager.php Class Class source

  Files folder image Files  /  lib  /  log  
File Role Description
  Plain text file FileLog.php Class Class source
  Plain text file LogInterface.php Class Class source

  Files folder image Files  /  lib  /  persistence  
File Role Description
  Plain text file DbDriver.php Class Class source
  Plain text file Driver.php Class Class source
  Plain text file ZHandlersocketDriver.php Class Class source

  Files folder image Files  /  lib  /  scaffold  
File Role Description
  Plain text file EntityGenerator.php Class Class source

  Files folder image Files  /  lib  /  test  
File Role Description
  Plain text file BaseTestCase.php Class Class source
  Plain text file PersistenceDriverTest.php Class Class source
  Plain text file TestEntity.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imagepersistence (2 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file EntityCollectionTest.php Class Class source
  Accessible without login Plain text file EntityTest.php Test Unit test script
  Accessible without login Plain text file SelectTest.php Test Unit test script
  Accessible without login Plain text file TxManagerTest.php Test Unit test script

  Files folder image Files  /  test  /  persistence  
File Role Description
  Accessible without login Plain text file DbDriverTest.php Test Unit test script
  Accessible without login Plain text file ZHandlersocketDriverTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:826
This week:1
All time:4,099
This week:468Up
User Ratings User Comments (1)
 All time
Utility:92%StarStarStarStarStar
Consistency:95%StarStarStarStarStar
Documentation:80%StarStarStarStarStar
Examples:90%StarStarStarStarStar
Tests:85%StarStarStarStarStar
Videos:-
Overall:81%StarStarStarStarStar
Rank:15