Database Abstraction

In order to query the Recite CMS database you will need the database abstraction object. You can retrieve it with the following code. Note that this call will never fail - a valid connection is guaranteed to be returned.

Example 3.7. Retrieving the active database connection

<?php
    $db = Application::GetDb();
?>

The database object that is returned from this call inherits from the Zend_Db_Adapter_Abstract class, meaning you can use the documentation at http://framework.zend.com/manual/en/zend.db.adapter.html as a reference.

Maintaining Cross-Platform Compatability

Recite CMS provides helper objects to maintain cross-database compatibility. For instance, to retrieve a formatted timestamp in MySQL the date_format() function is used, whereas PostgreSQL uses to_char().

To access the helper object for the current database connection, call $db->getStatementHelper().

The following methods are available from the returned object:

  • getRandomExpr(). Get the expression used for selecting or ordering by a random field. Returns Zend_Db_Expr.

  • getTimestamp($ts). Accepts a unix-timestamp (such as from PHP's time() function) and returns a value in a format that can be inserted into a database table.

  • unixTimestamp($val). Accepts a value from the database and returns a unix timestamp. Returns null

  • getOlderThanExpression($field, $qty, $unit, $orEqual = false). Get a database expression that returns true if the given database value is older than the passed interval. The $unit variable can be one of Zend_Date::SECOND, Zend_Date::MINUTE, Zend_Date::HOUR, Zend_Date::DAY, Zend_Date::MONTH or Zend_Date::YEAR. Returns an instanceof Zend_Db_Expr

  • getYoungerThanExpression($field, $qty, $unit, $orEqual = false). This is the same as the previous function except it returns an expression that returns true if the database value is younger than the given value.

  • getDateString($field). Returns a Zend_Db_Expr expression to fetch the given field in YYYY-MM-DD format.

  • getDateTimeString($field). Returns a Zend_Db_Expr expression to fetch the given field in YYYY-MM-DD HH:MM:SS format.