Disabling a Layout or View Renderer in a Zend Framework Controller

Written by James Mansson on October 13, 2013 Categories: Zend Framework 1

By default, in a Zend Framework 1 application, each action handler function in a controller class is assumed to have a corresponding view script. It is also usual to define a standard layout for pages, and apply that layout by default. However, there will be situations where you want to disable the layout, or even not make use of a view script. This could be because you are performing an AJAX action, or because the action simply performs a task before redirecting elsewhere.

To disable the layout, you make the following call in your action handler:

$this->_helper->layout->disableLayout();

To disable the view renderer, so you no longer need to create a view script, you make the following call in your action handler:

$this->_helper->viewRenderer->setNoRender(true);
No Comments on Disabling a Layout or View Renderer in a Zend Framework Controller

Getting and Setting a Checkbox Value in jQuery

Written by James Mansson on October 12, 2013 Categories: jQuery

Getting and setting a checkbox value is a common task in jQuery, which is simple to achieve when you know how.

Suppose we have a checkbox with an ID of active. To get the value of this checkbox as a 1 or 0 (checked or unchecked), we can use the following jQuery code:

var value = $("#active").is(":checked") ? 1: 0

To set the checkbox as checked or unchecked, there are two methods. The first works in all versions of jQuery, the second in newer versions (1.6 or later).

The older method sets the checkbox as checked by:

$("#active").attr("checked", "checked")

and sets it as unchecked by:

$("#active").removeAttr("checked")

The newer method uses:

$("'#active").prop("checked", isChecked)

where isChecked is true or false.

No Comments on Getting and Setting a Checkbox Value in jQuery

PHP empty Gotcha

Written by James Mansson on July 13, 2013 Categories: PHP

The PHP empty function is described as follows in the documentation:

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

Recently, I was working on some code where we used empty to validate a value passed via some XML. The value could be 0, 1 or 2, or it could be left blank. If it was specified, the corresponding database record would be updated with the new value. If it was left blank, that particular field would not be changed. We assumed that empty would detect whether a value had been passed at all, and so were confused that the system failed to set the field to 0 when we passed that value. It transpired that empty would exclude a value of 0 as well, as that evaluates to FALSE in PHP!

No Comments on PHP empty Gotcha

Specifying the Database Adapter to Use with the Zend Framework Db_NoRecordExists and Db_RecordExists Validators

Written by James Mansson on June 27, 2013 Categories: Databases, Zend Framework 1

In a previous post, I looked at using the Db_NoRecordExists and Db_RecordExists validators provided by the Zend Framework. As an addendum to that post, I would like to add a note on how to use these validators in a situation where the application works with multiple databases. (more…)

No Comments on Specifying the Database Adapter to Use with the Zend Framework Db_NoRecordExists and Db_RecordExists Validators

Removing the Zend_Form Default Method for Displaying Errors

Written by James Mansson on June 8, 2013 Categories: Zend Framework 1

The Zend_Form decorator system insists on displaying form errors as lists next to the field triggering the error. This is not always the behaviour that is desired. For instance, you may wish to display only the first error encountered, and show this above the form. In that case, you need to suppress the errors decorator for the fields where it applies. Fortunately, there is a simple method for doing this. Assuming that the form element is represent by the variable $element, we can make the following call:

$element->removeDecorator('Errors');

The element in question will now not display any associated errors next to it.

No Comments on Removing the Zend_Form Default Method for Displaying Errors

Generating a CSV File using PHP

Written by James Mansson on May 17, 2013 Categories: PHP

A common feature to add to a web application is the ability to export data. For tabular data, a sensible format is often a CSV file. The one complication is the need to escape the data properly. (more…)

No Comments on Generating a CSV File using PHP

Using a Paginator View Helper Partial in Another Module

Written by James Mansson on April 13, 2013 Categories: Zend Framework 1

In the Zend Framework, the Paginator View Helper looks by default for the partial specified under the views/scripts directory for the current module. If the partial is defined in one module (e.g. default) and used in another (e.g. admin), the module where it is located needs to be specified. For instance, you would use the following when rendering a pagination control defined in the default module from another module:

$this->paginationControl($this->paginator, 'sliding', 
  array('pagination_control.phtml', 'default'));

Note that rather than just specifying the partial file name as a string in the third parameter, we pass an array with the partial file name as the first entry, and module name as the second.

No Comments on Using a Paginator View Helper Partial in Another Module

Handling Connections to Multiple Databases in the Zend Framework

Written by James Mansson on March 29, 2013 Categories: Databases, Zend Framework 1

While many websites and web applications work with a single database, there are occasions when they need to work with more than one database. The Zend Framework provides a resource plugin called Zend_Application_Resource_Multidb, which enables multiple database connections to be configured via the application configuration file. (more…)

No Comments on Handling Connections to Multiple Databases in the Zend Framework

Retrieving the Highest Value in a Database Field using Zend_Db_Select

Written by James Mansson on March 16, 2013 Categories: Databases, Zend Framework 1

Sometimes you need to know the highest value in a particular database field. For instance, when inserting a new row into a table, you may need the value of a particular field to be one greater than the previous highest value in order to place the record next in sequence.

Assuming that we have a Zend_Db_Table object $dbTable and that the field in questions is called sequence, we could use the following code to generate the next value:

$select = $dbTable->select();
$select->from($this, array('MAX(`sequence`) as s'));
$rows = $dbTable->fetchAll($select);
$sequence = $rows[0]->s + 1;

We start by retrieving a Zend_Db_Select object, which can be used to construct the query. The call to the MySQL MAX function is specified as the field we wish to retrieve, with s being used as the alias to the field. When we retrieve the value, we add one to it. This value can then be used when inserting the new row.

No Comments on Retrieving the Highest Value in a Database Field using Zend_Db_Select

Zend Framework Access Control using a Controller Plugin

Written by James Mansson on March 13, 2013 Categories: Access Control, AJAX, Zend Framework 1

Perhaps the simplest way to enforce access control in a Zend Framework application is to use a controller plugin. Controller plugins are a mechanism for executing code at certain points during the controller process. In our case, we want to check whether a user’s request should be permitted before the request is transferred to the appropriate action handler. (more…)

No Comments on Zend Framework Access Control using a Controller Plugin