Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 ...
Beginner++
PDO
● PRO
– Lightweight, consistent
– Can connect to different databases
– Prepared statements
– Better security through n...
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 loa...
ORM
● ORM does all the heavy lifting when working with databases
– Programmer just uses objects
● Searching, updating, del...
ORM OO patterns
● Active Record
– In-memory object carries both data and the functions
needed to work with database
● Data...
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
● CR...
PHP ORMs
● Active Record
– Eloquent (Laravel)
– Doctrine 1
– DataMapper ORM library (CodeIgniter)
– Propel
● Data Mapper
–...
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 caus...
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 ...
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 probab...
Conclusion
● Idiorm is great for
– Beginners who are used to think about tables and joins
through SQL
● Usually brainwashe...
Upcoming SlideShare
Loading in …5
×

Achieve the norm with Idiorm

637 views

Published on

Talk at ZGPHP meetup #50
Video of the talk:
http://zgphp.org/videos/zgphp-stipe-predanic-achieving-the-norm-with-idiorm/

http://zgphp.org/
http://www.meetup.com/ZgPHP-meetup/events/225442690/


Published in: Technology
  • Be the first to comment

  • Be the first to like this

Achieve the norm with Idiorm

  1. 1. Achieve the norm with Idiorm Stipe Predanić University of Applied sciences, Zagreb
  2. 2. Old school (Beginner)
  3. 3. Beginner+
  4. 4. 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
  5. 5. Beginner++
  6. 6. 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
  7. 7. What about OO?
  8. 8. Yes, really, what about OO?
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. ORM OO patterns Active Records Data Mapper
  13. 13. 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
  14. 14. PHP ORMs ● Active Record – Eloquent (Laravel) – Doctrine 1 – DataMapper ORM library (CodeIgniter) – Propel ● Data Mapper – Doctrine 2
  15. 15. Eloquent Direct examples from Eloquent documentation: http://laravel.com/docs/5.1/eloquent
  16. 16. Eloquent ● Eloquent supports relationships and can query according to them
  17. 17. Eloquent & Laravel ● Neatly coupled ● Schema Builder – Migrations
  18. 18. Doctrine 2 Direct examples from Doctrine documentation: http://doctrine-orm.readthedocs.org/
  19. 19. Doctrine 2 Direct examples from Doctrine documentation: http://doctrine-orm.readthedocs.org/
  20. 20. Doctrine 2 ● Has relationships and way more ● Slightly higher learning curve ● But blazing fast for an ORM
  21. 21. 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
  22. 22. Idiorm Example of simple select, update, insert and delete
  23. 23. Idiorm configuration
  24. 24. Idiorm querying
  25. 25. Idiorm ORM object
  26. 26. Idiorm – how to get data out
  27. 27. Idiorm querying cont. Taken from documentation: https://idiorm.readthedocs.org/en/latest/querying.html Getting many in an array Getting many in ORM objects
  28. 28. Idiorm - joins ● Joins are ... clunky From documentation
  29. 29. Idiorm – Active Record ● There is an Active Record implementation based on Idiorm called Paris From documentation
  30. 30. 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!
  31. 31. 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

×