Article

eZ publish: PHP's Killer App - Parts 1-3

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Next

Total Class

Reaching for the oven: "Here's one I prepared earlier"...

Breaking the finished class down, first we have:

<?php                
//!! eZFaq                
//! Base class for public use                
               
// Include the "abstract" eZ publish classes */                
include_once( "classes/ezdb.php" );                
include_once( "classes/ezdate.php" );                
include_once( "classes/ezdatetime.php" );                
include_once( "classes/ezlocale.php" );                
               
class eZFaq {

The first thing we need to do is make some eZPublish base classes available by including them, namely: eZDb, eZDate, eZDateTime and eZLocale (have a glance at the documentation so you know what they do).

Next:

   // CREATORS                
   /*!                
     Constructs a new eZFaq object.                
               
     If $id is set the object's values are fetched from the                
     database.                
   */                
   function eZFaq( $id=-1 ) {                
       if ( $id != -1 ) {                
           $this->ID = $id;                
           $this->get( $this->ID );                
       }                
   }

The constructor function eZFaq (with the same name as the class) does something very interesting. If it receives an $id (of a FAQ from the database), it assigns the value to a membevariable, and then invokes the get() member function (which, you remember, fetches a single FAQ from the database).

   // MANIPULATORS                
   /*!                
    Gets a single faq from the database                
   */                
   function get( $id=-1 ) {                
     $db =& eZDB::globalDatabase();                
     $ret = false;                
     if ( $id != -1  ) {                
        $db->array_query($faq_array,                
                         "SELECT * FROM ezfaq_faq ".                
                         "WHERE ID='$id'" );                
        if ( count( $faq_array ) > 1 ) {                
            die( "Error: Duplicate faq ID found in database.".                
                 "This shouldn't happen." );                
        } else if ( count( $faq_array ) == 1 ) {                
            $this->ID =& $faq_array[0][$db->fieldName("ID")];                
            $this->Question =& $faq_array[0][$db->fieldName("Question")];                
            $this->Answer =& $faq_array[0][$db->fieldName("Answer")];                
            $this->Date =& $faq_array[0][$db->fieldName("Date")];                
            $this->Published =                
                & $faq_array[0][$db->fieldName("Published")];                
            $this->HeadingID =                
                & $faq_array[0][$db->fieldName("HeadingID")];                
            $ret = true;                
        }                
    }                
    return $ret;                
  }

Now the get() method is used to fetch a single FAQ from the database. It starts off by instantiating an eZDB object which it can then use to perform a query and fetch the results. eZ systems have pulled some clever tricks in the background to allow us to use a static method from the eZDB class. We won't worry about that here, but suffice it to say that because the eZ publish documentation is good, we can just use the class without caring about what's going on behind the scenes. That's one of the joys of object orientation!

The get() method then queries the database to fetch a single row and assign the results to local member variables (e.g. $this->Question).

Moving on...

   /*!                
    Gets a list of faqs from the database                
   */                
   function &getAll( $headingID = -1 ) {                
       $db =& eZDB::globalDatabase();                
                       
       $return_array = array();                
       $result_array = array();                
               
       if ($headingID != -1 ) {                
           $where = "WHERE HeadingID = '$headingID' ";                
       }                
                       
       $db->array_query($result_array,                
                "SELECT ID ".                
                "FROM ezfaq_faq  ".                
                $where.                
                "ORDER BY Question" );                
                       
       for ( $i=0; $i<count($result_array); $i++ ) {                
           $return_array[$i]=                
             new eZFaq($result_array[$i][$db->fieldName("ID")] );                
       }                
       return $return_array;                
   }

The getAll() method fetches a list of FAQs from the database. If it receives a "headingID", it adds a conditional WHERE to the query, fetching only those FAQs for a heading we're interested in.

The really cunning part is here though:

$return_array[$i]=new eZFaq($result_array[$i][$db->fieldName("ID")] );

See what that's doing? The getAll() method is instantiating the eZFaq class itself, using the FAQ ID if found for each row it selected. Now have a look at the constructor method eZFaq() again. Making sense now? For each row that getAll() finds, the constructor calls the get() method which performs another query to fetch all the details for that row. What this does for us is place the all code for handling a single row within a single method. Now imagine we change the ezfaq_faq table at some later date, to add a new column: using this approach, for our MANIPULATORS, we only need to make one change!

Next we have:

   /*!                
    Searches the ezfaq_faq table for a string                
   */                
   function &search($searchstring) {                
       $db =& eZDB::globalDatabase();                
                       
       $return_array = array();                
       $result_array = array();                
                       
       $db->array_query($result_array,                
                        "SELECT ID, Question, Date ".                
                        "FROM ezfaq_faq WHERE ".                
                        "MATCH(Question,Answer) ".                
                        "AGAINST ('$searchstring') " .                
                        "ORDER BY Question" );                
                       
       for ( $i=0; $i<count($result_array); $i++ ) {                
           $return_array[$i] =                
             new eZFaq( $result_array[$i][$db->fieldName("ID")] );                
       }                
       return $return_array;                
   }

The search() method is very similar to the getAll() method, except that it accepts $searchstring, which is use to perform a FULLTEXT search against the FULLTEXT index we defined in the database. In addition to the recommended reading above, on FULLTEXT searching, check out Optimizing your MySQL Application as well, to find out more about indexes.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links