Building Data Mapper PHP5

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    5 Favorites

    Building Data Mapper PHP5 - Presentation Transcript

    1.  
    2. “ 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
    3. 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
    4.  
      • 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?
    5.  
      • 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?
    6.  
    7.  
      • 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
    8. 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
    9.  
      • $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
    10.  
    11.  
      • 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()
    12.  
    13.  
    14.  
      • 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
    15.  

    + vlucasvlucas, 2 years ago

    custom

    3419 views, 5 favs, 1 embeds more stats

    See all the steps involved to build a fully functio more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 3419
      • 3407 on SlideShare
      • 12 from embeds
    • Comments 0
    • Favorites 5
    • Downloads 82
    Most viewed embeds
    • 12 views on http://www.vancelucas.com

    more

    All embeds
    • 12 views on http://www.vancelucas.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories