MySQL Expressions in Zend Framework

If you’re a Zend Framework developer, you’d know the built-in Zend_Db_Table methods insert(), update(), etc.

However the general approach there is like is said:

$data = array();
$data['key'] = $value;
$model = new ModelName();
$model->insert($data);
 
// that's very basic example, but in general this is 
// the way it works.

What happens if one of the column have to be an MySQL expression!? In example you’ve a date field and you’d like to insert something like the MySQL’s method NOW().

The solution is simply call a Zend_Db_Expr.

$data = array();
$data['key'] = $value;
$data['date'] = new Zend_Db_Expr('NOW()');
$model = new ModelName();
$model->insert($data);
 
// now the date column will be NOWed :)

Related posts:

  1. Zend_Validate_Db_RecordExists in Zend Framework 1.10+
  2. Zend_Date::setOptions and format_type in Zend Framework 1.10.3
  3. JSON and Zend Framework? – Zend_Json
This entry was posted in micro tutorial, PHP, zend framework and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">