Tag Archives: MySQL

Zend Framework: Connect MySQL

There are so many tutorials about that, but let me add it one more time. You’ve to use the PDO_MYSQL adapter:

// where params include adapter, host
// username, password and dbname
$db = new Zend_Db_Adapter_Pdo_Mysql($params);

and set the default adapter

Zend_Db_Table::setDefaultAdapter($db);

That can be done in the bootstrap or into a front controller plugin!

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 :)