HomePHP 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).
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.