Home arrow PHP arrow Page 5 - The Active Record Pattern, concluded

Issues - PHP

This article, the second of two parts, helps you use design patterns to better organize how your web application interacts with a database. It is excerpted from chapter 14 of the book php|architect's Guide to PHP Design Patterns, written by Jason E. Sweat (php|architect, 2005; ISBN: 0973589825).

  1. The Active Record Pattern, concluded
  2. Active Record Instance ID
  3. Searching for Records
  4. Updating Records
  5. Issues
By: php|architect
Rating: starstarstarstarstar / 2
December 29, 2005

print this article



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: