Article
eZ publish: PHP's Killer App - Parts 1-3
Next, the store() method:
// MANIPULATORS
/*!
Inserts or updates a faq
*/
function store() {
$db =& eZDB::globalDatabase();
$Question = $db->escapeString( $this->Question );
$Answer = $db->escapeString( $this->Answer );
$timeStamp =& eZDateTime::timeStamp( true );
$Published = ( $this->Published );
$HeadingID = ( $this-HeadingID );
$db->begin();
$db->lock( "ezfaq_faq" );
$nextID = $db->nextID( "ezfaq_faq", "ID" );
if ( empty( $this->ID ) ) {
$ret = $db->query( "INSERT INTO ezfaq_faq SET ".
"ID = '$nextID', Question = '$Question', ".
"Answer = '$Answer', Date = '$timeStamp', ".
"HeadingID = '$HeadingID', Published='$Published'" );
$this->ID = $nextID;
} else {
$ret = $db->query( "UPDATE ezfaq_faq SET ".
"ID = '$nextID', Question = '$Question', ".
"Answer = '$Answer', Date = '$timeStamp', ".
"HeadingID = '$HeadingID', Published='$Published' " .
"WHERE ID='$this->ID'" );
}
$db->unlock();
if ( $ret == false )
$db->rollback();
else
$db->commit();
return $ret;
}
The store() method allows us to both INSERT and UPDATE records in the ezfaq_faq table, depending on whether it finds that the member variable $id has been set (remember, the child inherits everything from the parent; member functions and variables).
Near the start of the store() method we have:
$Question = $db->escapeString( $this->Question );
$Answer = $db->escapeString( $this->Answer );
$timeStamp =& eZDateTime::timeStamp( true );
Anyone who's run into trouble with quotes contained in strings that they want to place in a database will be relieved to see $db->escapeString(). And those of your concerned about MySQL and ANSI SQL will be happy to see how eZ publish escapes strings to be stored in Informix. Basically what's happening here is we're transforming the data.
Note that eZDateTime::timeStamp( true ); creates a UNIX time stamp for the current time, which we can use to tell our visitors when a FAQ was published.
As to the database locking, rollback and committing that's going on here, I'll let you compare the relevant class documentation for MySQL, PostGRESQL and Informix. In general, all you'll need to do is copy and paste what I've done here for SQL INSERTs, UPDATESs and DELETEs.
/*!
Deletes a faq
*/
function delete( $id ) {
$db =& eZDB::globalDatabase();
if ( is_numeric( $id ) ) {
$this->ID = $id;
}
$db->begin();
$ret = $db->query(
"DELETE FROM ezfaq_faq WHERE ID='$this->ID'" );
if ( $ret == false )
$db->rollback( );
else
$db->commit( );
return $ret;
}
The delete() method is pretty straightforward. It uses the ezDB class to delete a record from the ezfaq_faq table.
And finally we have the ACCESSORS:
// ACCESSORS
/*!
Assigns faq question to local member variable
for store() method
*/
function setQuestion($Question) {
$this->Question = $Question;
}
/*!
Assigns faq answer to local member variable
for store() method
*/
function setAnswer($Answer) {
$this->Answer = $Answer;
}
/*!
Assigns the Published switch for the store() method
*/
function setPublished ($Published) {
$this->Published = $Published;
}
/*!
Sets the current heading id
*/
function setHeadingID ($HeadingID) {
$this->HeadingID = $HeadingID;
}
}
These assign the variables they receive to member variables defined in the parent eZFaq class. So, when we need to INSERT some data into the database, we first make use of these methods, and then use the store() method to add them.
And that's it: the logic for our module is done! The eZFaqHeading and eZFaqCategory classes are very similar, so I'll leave them to the ZIP file at the end of this article (they should be straightforward to understand, now that you've seen the eZFaq class). Note that I've used the same member function names in those classes as well (for example the eZFaqCategory class also has a method called getAll()).
Now it's time to plug our classes into the eZ publish framework...