31
Achieve the norm with Idiorm Stipe Predanić University of Applied sciences, Zagreb

Achieve the norm with Idiorm

Embed Size (px)

Citation preview

Page 1: Achieve the norm with Idiorm

Achieve the norm with Idiorm

Stipe PredanićUniversity of Applied sciences, Zagreb

Page 2: Achieve the norm with Idiorm

Old school (Beginner)

Page 3: Achieve the norm with Idiorm

Beginner+

Page 4: Achieve the norm with Idiorm

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

Page 5: Achieve the norm with Idiorm

Beginner++

Page 6: Achieve the norm with Idiorm

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

Page 7: Achieve the norm with Idiorm

What about OO?

Page 8: Achieve the norm with Idiorm

Yes, really, what about OO?

Page 9: Achieve the norm with Idiorm

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

Page 10: Achieve the norm with Idiorm

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

Page 11: Achieve the norm with Idiorm

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

Page 12: Achieve the norm with Idiorm

ORM OO patterns

Active Records Data Mapper

Page 13: Achieve the norm with Idiorm

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

Page 14: Achieve the norm with Idiorm

PHP ORMs

● Active Record

– Eloquent (Laravel)

– Doctrine 1

– DataMapper ORM library (CodeIgniter)

– Propel

● Data Mapper

– Doctrine 2

Page 15: Achieve the norm with Idiorm

Eloquent

Direct examples from Eloquent documentation: http://laravel.com/docs/5.1/eloquent

Page 16: Achieve the norm with Idiorm

Eloquent

● Eloquent supports relationships and can query according to them

Page 17: Achieve the norm with Idiorm

Eloquent & Laravel

● Neatly coupled

● Schema Builder

– Migrations

Page 18: Achieve the norm with Idiorm

Doctrine 2

Direct examples from Doctrine documentation: http://doctrine-orm.readthedocs.org/

Page 19: Achieve the norm with Idiorm

Doctrine 2

Direct examples from Doctrine documentation: http://doctrine-orm.readthedocs.org/

Page 20: Achieve the norm with Idiorm

Doctrine 2

● Has relationships and way more

● Slightly higher learning curve

● But blazing fast for an ORM

Page 21: Achieve the norm with Idiorm

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

Page 22: Achieve the norm with Idiorm

Idiorm

Example of simple select, update, insert and delete

Page 23: Achieve the norm with Idiorm

Idiorm configuration

Page 24: Achieve the norm with Idiorm

Idiorm querying

Page 25: Achieve the norm with Idiorm

Idiorm ORM object

Page 26: Achieve the norm with Idiorm

Idiorm – how to get data out

Page 27: Achieve the norm with Idiorm

Idiorm querying cont.

Taken from documentation: https://idiorm.readthedocs.org/en/latest/querying.html

Getting many in an array

Getting many in ORM objects

Page 28: Achieve the norm with Idiorm

Idiorm - joins

● Joins are ... clunky

From documentation

Page 29: Achieve the norm with Idiorm

Idiorm – Active Record

● There is an Active Record implementation based on Idiorm called Paris

From documentation

Page 30: Achieve the norm with Idiorm

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!

Page 31: Achieve the norm with Idiorm

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