Upload
stipe-predanic
View
583
Download
0
Embed Size (px)
Citation preview
Achieve the norm with Idiorm
Stipe PredanićUniversity of Applied sciences, Zagreb
Old school (Beginner)
Beginner+
Mysqli (native extension)
● PRO
– Fast
● CON
– Hand written queries
– Rewriting all DB calls when changing database
– Not OO friendly
– Manual track of inserts and updates
Beginner++
PDO
● PRO
– Lightweight, consistent
– Can connect to different databases
– Prepared statements
– Better security through named parameters (escaped by PDO)
● CON
– still hand written SQL (different vendors have different dialects)
– still no good OO support
What about OO?
Yes, really, what about OO?
ORM
● Object relation mapping
– Object properties and their relationships are preserved when saving to database and on load back from DB
● In short:
– Data from database table(s) are mapped to a class
– Data from classes are mapped to database table
ORM
● ORM does all the heavy lifting when working with databases
– Programmer just uses objects
● Searching, updating, deleting are done by simple commands instead of using SQL
● ORM gives persistency to objects.
● Most PHP ORMs use PDO in the background
ORM OO patterns
● Active Record
– In-memory object carries both data and the functions needed to work with database
● Data Mapper
– In-memory object just represents data
– Additional Mapper is used to separate database and objects, and connects them
● One Mapper can work with several classes
ORM OO patterns
Active Records Data Mapper
ORM OO patterns use
● Active record
– Mostly used when object represents one or a few tables in DB
● Database mindset● CRUD
● Data mapper
– Mostly used when object is scattered in several tables in DB
PHP ORMs
● Active Record
– Eloquent (Laravel)
– Doctrine 1
– DataMapper ORM library (CodeIgniter)
– Propel
● Data Mapper
– Doctrine 2
Eloquent
Direct examples from Eloquent documentation: http://laravel.com/docs/5.1/eloquent
Eloquent
● Eloquent supports relationships and can query according to them
Eloquent & Laravel
● Neatly coupled
● Schema Builder
– Migrations
Doctrine 2
Direct examples from Doctrine documentation: http://doctrine-orm.readthedocs.org/
Doctrine 2
Direct examples from Doctrine documentation: http://doctrine-orm.readthedocs.org/
Doctrine 2
● Has relationships and way more
● Slightly higher learning curve
● But blazing fast for an ORM
Idiorm
● Idiorm is a deliberetely simple ORM
– Micro-ORM
– Pareto Principle● 80% of the effects come from 20% of the causes
● Idiorm has only one class called ORM
● Uses PDO
● IS NOT A FULL ACTIVE RECORD ORM
Idiorm
Example of simple select, update, insert and delete
Idiorm configuration
Idiorm querying
Idiorm ORM object
Idiorm – how to get data out
Idiorm querying cont.
Taken from documentation: https://idiorm.readthedocs.org/en/latest/querying.html
Getting many in an array
Getting many in ORM objects
Idiorm - joins
● Joins are ... clunky
From documentation
Idiorm – Active Record
● There is an Active Record implementation based on Idiorm called Paris
From documentation
Conclusion
● Use ORM
– Any ORM● Just use it
● If a programmer is experienced and uses a framework – that framework probably uses some kind of an ORM
● Almost all of them are great!
Conclusion
● Idiorm is great for
– Beginners who are used to think about tables and joins through SQL
● Usually brainwashed students
– Small projects where no special set-up is wanted or needed
– For old projects which still uses MySQL statements, as Idiorm is a simple drop-in solution