Tag Archives: zend framework

Debugging in Zend Framework

Perhaps “debugging” is a bit too strong. However when you’re dumping an array in PHP, you’d probably prefer the print_r or var_dump.

echo '<pre>';
print_r($array);
echo '</pre>';

But did you know that in Zend Framework there’s a built in Zend_Debug?

Zend_Debug::dump($array);

Does pretty much the same thing!

Zend_Date::setOptions and format_type in Zend Framework 1.10.3

I recently upgraded from the old Zend Framework 1.9.5 to the latest – 1.10.3. Everything just looked to be working quite fine, except the date formatting. Although I had the same date formatting and options in the old version it seems to be not working.

I had the format_type set to PHP and even though I formatted the dates with a Zend Framework specific format: “d MMM yyyy”.

Zend_Date::setOptions(array('format_type' => 'php'));
// ...
$date = new Zend_Date($somedate);
echo $date->get('d MMM yyyy');

Now as it appears format_type php in 1.10.3 is giving completely different result, and the correct format is date() like format in PHP.

Zend_Date::setOptions(array('format_type' => 'php'));
// ...
$date = new Zend_Date($somedate);
echo $date->get('d M yy');

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

Detecting POST Requests in Zend Framework

Pure PHP

Pure phpiers are using used to something like detecting a submit in the _POST array.

if (isset($_POST['submit']) { ... }

Of course for this you’ve to be sure the HTML contains an submit type element with name attribute equal to “submit”.

The Zend Framework’s Way

Simply replace the line above with:

if ($this->getRequest()->isPost()) { ... }

This is way better than the first example. It detects the request method, not an array element!?