PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
Professional Linux Programming

Professional Linux Programming

As Linux increases its presence throughout the world as a target platform for professional...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Managing Your Data With DatabaseObject

Custom Loaders

By default, the only way to load a record with DatabaseObject is to use the loadRecord() method. This loads a record based on the primary key field you pass in the child class’s constructor. You may want to load a record based on a username instead. This is also very easy to achieve. Basically, you build your database query, then pass it to the internal _loadRecord() method, like so:

Listing 12 listing-12.php
<?php
    class User extends DatabaseObject
    {
        /* ... other code ... */
 
        function loadRecordByUsername($username)
        {
            $query = sprintf("select %s from %s where username = '%s'",
                             join(', ', $this->getSelectFields()),
                             $this->table,
                             $this->escape($username));
 
            return $this->_loadRecord($query);
        }
    }
 
    $user = new User($db);
    $user->loadRecordByUsername('johndoe');
?>

You’ll notice the getSelectFields() method was also used. This is just a utility method to build an array of the fields in the database table. So all we do is join this list with a comma so we can select it. You could just use * instead (select * from …), but if you were to create a more advanced loader, you may perform a join to load the record, in which case you would only want to select fields from the main table (not the joined table), so this method is useful in that case.

For example, let’s say you wanted to load the first record (alphabetically by username) that linked to, say, PhpRiot (www.phpriot.com). To do this, you would need to use both the users and users_links tables to determine this, but you only want the database from the users table.

Listing 13 listing-13.php
<?php
    class User extends DatabaseObject
    {
        /* ... other code ... */
 
        function loadRecordThatLinksTo($url)
        {
            $query = sprintf("
                                select %s from users u, users_links l
                                    where l.user_id = u.user_id and l.url = '%s'
                                    order by lower(u.username) limit 1
                             ",
                             join(', ', $this->getSelectFields('u.')),
                             $this->escape($username));
 
            return $this->_loadRecord($query);
        }
    }
 
    $user = new User($db);
    $user->loadRecordThatLinksTo('http://www.phpriot.com');
?>

Note here that getSelectFields() took an argument in this case. This was a field prefix (so say username would come back as u.username since we aliased the users table by u).

In This Article


Tagged in , ,

Additional Files