Building Data Mapper PHP5
Upcoming SlideShare
Loading in...5
×
 

Building Data Mapper PHP5

on

  • 20,708 views

See all the steps involved to build a fully functional data mapper with object-oriented PHP5 using the power and simplicity of the Standard PHP Library. Based on the early development of a new PHP ...

See all the steps involved to build a fully functional data mapper with object-oriented PHP5 using the power and simplicity of the Standard PHP Library. Based on the early development of a new PHP DataMapper project Vance Lucas started a few months ago as the Model layer for an MVC framework.

Statistics

Views

Total Views
20,708
Views on SlideShare
20,569
Embed Views
139

Actions

Likes
25
Downloads
405
Comments
3

6 Embeds 139

http://www.slideshare.net 99
http://www.vancelucas.com 32
http://www.linkedin.com 3
https://www.linkedin.com 2
http://www.slideee.com 2
http://127.0.0.1:12006 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

13 of 3 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Building Data Mapper PHP5 Building Data Mapper PHP5 Presentation Transcript

    •  
    • “ 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 Loading Always loads relations with row Lazy Loading Loads 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 mapper with “__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_address]
      • 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
    •