PHP
  Home arrow PHP arrow Page 5 - The Active Record Pattern, concluded
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Active Record Pattern, concluded
By: php|architect
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2005-12-29


    Table of Contents:
  • The Active Record Pattern, concluded
  • Active Record Instance ID
  • Searching for Records
  • Updating Records
  • Issues

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Active Record Pattern, concluded - Issues
    ( Page 5 of 5 )

    The Active Record pattern is simple in both concept and execution and probably represents what most initial attempts to refactor from procedural coding to object-oriented programming would look like. It’s nice to have all of your SQL code grouped into a single location and the Active Record pattern gives you a nice way to couple business logic with database access to persist the object.

    The example in this chapter used an actual database to develop and test the code. Another way to test simple database code is to use Mock Objects (see Chapter 6) to completely simulate the database connection. Unfortunately though, this approach does not scale. SQL is a complex language and mocking individual statements tightly couples tests with database specifics.  Using freshly-created, actual tables provide a higher degree of comfort, without the brittle effects of Mocking SQL.

    If there’s a downside to the Active Record pattern, it’s complexity. An Active Record class can grow quite quickly—it attracts features like a magnet. For example, the Bookmark classes only included a findById() method, but you’d  likely also want findByUrl(), findByDescription(), findByGroup(), findRecentlyCreated(), and so on.

    Another issue, which is possible to see in the testing of the save() method, is that objects can become “duplicated.” For example, $link and $link2 in the  test case aren’t the same objects, though they both refer to the same bookmark ID. You could test this explicitly also:

    class ActiveRecordTestCase extends UnitTestCase {
      // ...
      function testSave() {
        // ...
        $this->assertNotIdentical($link, $link2);
      }
    }

    If it’s important to work around this issue, you might want to add an internal Registry (see Chapter 5) to make sure all instances of the object returned by Bookmark(1) are in fact the same object. Because you’re actually using the new operator to create the objects instead of a Factory method, you might have to restructure the Bookmark class as a Proxy (see Chapter 11) to the actual Active Record class to really pull this off.

    Another aspect of the Active Record pattern is that it is designed to work with data one row at a time.  This is fairly typical of “admin” screens for applications where you might be editing an article, a link, a comment or any other row from a database, but a good deal of web pages deal with result sets or combinations of rows, which is more the domain of our next chapter—The Table Data Gateway Pattern.



     
     
    >>> More PHP Articles          >>> More By php|architect
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT