PHP
  Home arrow PHP arrow Page 3 - 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 - Searching for Records
    ( Page 3 of 5 )

    At the moment, a Bookmark can be stored in a database and can be (re)created by retrieving the database row that matches the bookmark’s ID.  But what happens—as is usually the case—when the ID is not known or you want to search the database for a more pertinent value, such as a partial name or a URL.  A common solution is to add “finder” methods. 

    For example, you might want a  findByUrl() method to find Bookmarks similar to the parameter passed to the method. Here’s that intention expressed as a test:

    class ActiveRecordTestCase extends UnitTestCase {
      // ...
      function testFindByUrl() {
        $this->add(‘http://blog.casey-sweat.us/’, ‘My Blog’,
          ‘Where I write about stuff’, ‘php’);
        $this->add(‘http://php.net’, ‘PHP’, 
          ‘PHP Language Homepage’, ‘php’);
        $this->add(‘http://phparch.com’, ‘php|architect’, 
          ‘php|arch site’, ‘php’);
        $result = Bookmark::findByUrl(‘php’);
        $this->assertIsA($result, ‘array’);
        $this->assertEqual(2, count($result));
        $this->assertEqual(2, $result[0]->getId());
        $this->assertEqual(‘php|architect’, $result[1]->name);
      }
    }

    The test creates some data, searches for rows that contain “php” somewhere in the URL, and then verifies characteristics of the returned array of Bookmark objects. FindByUrl() is a static method, because you want Bookmark objects, but do not yet have an instance of the Bookmark class to work with. (Alternatively, you could move these “finder” methods to an object of their own, but for now the finder methods are a part of the Active Record Bookmark class.)

    Here’s some code to realize the requirements expressed by the test:

    class Bookmark {
      // ...
      const SELECT_BY_URL = “
        select id
        from bookmark
        where url like ?”;
      public static function findByUrl($url) {
        $rs = DB::conn()->execute(
          self::SELECT_BY_URL
          ,array(“%$url%”));
        $ret = array();
        if ($rs) {
          foreach ($rs->getArray() as $row) {
            $ret[] = new Bookmark($row[‘id’]);
          }
        }
        return $ret;
      }
    }



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT