Building Data Mapper PHP5
-
Upload
vance-lucas -
Category
Technology
-
view
16.316 -
download
2
description
Transcript of Building Data Mapper PHP5
“A layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself”
Martin Fowler, Patterns of Enterprise Application Architecture
Object-Relational Mapping (ORM) Objects => Relational Database Relational Database => Objects Think “Translation layer”
Objects have no knowledge of database or SQL
Database has no knowledge of objects Objects have no knowledge of mapper
Allows you to work with native objects in your application
Writing SQL statements for everything in your application can be tedious INSERT, UPDATE especially
Reduce time working on projects
Ultra Portable No dependencies on packages outside PHP5
Use standard naming conventions Zend/PEAR Coding Standards
Simple, clear syntax that fits with PHP Generic Usage
Can work with any table by extending Eliminate the need for custom SQL (in most
cases) Enable full automation wherever possible
Row objects as “dumb” as possible Not tied to the data mapper
Allow custom SQL when needed for flexibility
Support table relations Use adapters for database independence Don’t depend on too much “magic”
No automatic discovery of tables or fields No table or column naming schemes for relations
CodeIgniter DataMapper: http://stensi.com/datamapper/
What objects will we need to start? DataMapper
Result – Individual row object
Start with DataMapper Define basic public interface
What table are we working with? All finders generate SQL
They can use “findBySql” internally get() needs a primary key field defined findFirst() can use find() with a LIMIT clause save() can use insert() or update()
internally insert() and update() both need
fields/values Which fields exist in the table?
Required fields Built-in validation? How do we handle errors?
Field types Cast to specified type or validate
automatically? Table relations
How do we handle table relations? Loading an external mapper – how?
Finders must return FALSE or iterate Results must be able to be in foreach() loop
Relations accessible by set field alias Implementation defined ‘contacts’ $user->contacts should have the related
fields Relations must also return FALSE or iterate
as well as be able to be count()-ed
Eager Loadin
gAlways loads
relations with row
Lazy Loadin
gLoads
relations only when
asked
Wait… How? $user->contacts can’t be an array $user object does not know about mapper
Can’t pull in relations dynamically from mapperwith “__get” magic method
Are we stuck with eager loading?
Relation Object as a placeholder empty until called
Relation Object uses SPL interfaces IteratorAggregate, Countable
foreach() – can iterate count($user->contacts) will work
Use SPL functions to fetch results upon request
$user->contacts foreach() calls ‘getIterator()’
(IteratorAggregate) Calls findAllRelation(), returns ResultSet object
count() calls ‘count()’ (Countable) Calls findAllRelation(), returns count of found
rows
findAllRelation() calls findAll(), caches result findAll() gets related rows from related mapper
with given foreign keys as conditions
Adapters Adapter Interface One Initial adapter to test with
Adapter_PDO_MySQL
Relations Relation Interface Relation_HasMany
PHPDataMapper _Database
_Adapter _PDO
_Mysql
_Exception _Model
_Relation _HasMany
_ResultSet _Result
Why not a generic ‘stdClass’ or an array? A Result object lets us:
Keep track of changed fields for update() Use object syntax – $row->name;
Suppress undefined property errors with __get() Use array syntax – $row[‘name’];
SPL ArrayAccess interface Suppress undefined index errors with offsetGet()
Use getter functions - $row->getName(); Automatic functions for all fields with __call()
Simple syntax to get almost any record All results returned are “live” objects
Can be altered and saved at any time No writing SQL code for mundane
operations Promotes good programming style
Easier to always use mapper object than write SQL queries all over the place
Project Page on my blog www.vancelucas.com/phpdatamapper
Open-Source Project MIT License On Google Code
Martin Fowler’s “P of EAA” http://martinfowler.com/eaaCatalog/
Vance Lucas, 23 Blog: www.vancelucas.com Email: [email protected]
Making websites since age 12 Started learning PHP when PHP3 was new
Oklahoma Baptist University – Shawee Bachelor of Business Administration
Currently work at Back40Design, Inc. www.back40design.com