Category Archives: zend-framework

Extending Zend Form Element to create customized Phone number field

Generally, Phone numbers has different parts. Such as, country code, operator code and subscriber number etc. When taking Phone number as user input, we can worn users about phone number format by setting a hint/description and can validate using Regular Expression. But, for better usability, along with hints, some web applications provide separate (generally 3) text fields for different parts of phone number. Also, some of them keep a separator, ‘-‘ or ‘#’ between this fields.

Now, if we try provide this feature in Zend Form, that’s possible. We can create three individual Zend_Form_Element_Text objects and join there value together to make the phone number. But, in this case, validating them together is a hassle. Again, placing them inline in the form and putting separators needs some Decorator magic. What if we can make it simply another Zend Form Element which renders three text box (optionally with separator)? And consider their value jointly and validate them together using ordinary Zend Validators? Lets try.

What we are going to make?

Let’s visualize our Phone number field first and list what we want from it.

  • It’s Simple. It’s rendering, validation everything is like any other ordinary Zend From Elements.
  • It prints 3 text boxes for different parts of Phone number.
  • When we validate it or take it’s value, the input of these three boxes jointly will be considered as the value.
  • We can set a separator character to be printed between text boxes.
  • We can include/exclude this separators from value.

Continue reading

Taking Zend Framework workshop on Dec, 09

Bbjobs has arranged a workshop on “Zend framework fundamental” on December, 09  and invited us (me and Emran Hasan) to take it. We agreed happily. This is (so far I know) the first formal Zend Framework workshop in Bangladesh. The workshop has been defined as –

The Zend Framework Workshop is a 5 half-day tutorial-style course that takes an in-depth look at Zend Framework and its components. The workshop is designed for experienced PHP programmers who want to learn to combine ZF concepts and structural elements to utilize the full power of this software development kit for PHP 5 applications.

As per our plan about the context of this workshop, we are going to cover the following 8 basic topics of Zend Framework in 5 days.

Continue reading

Advanced bootstrapping : Configure your Zend Framework application for multiple host

This tutorial is intended to explain a way of bootstrapping Zend Framework based MVC application for multiple host/domains. Before starting, let me explain a bit about the situation when it’s needed.

A web application goes through some stages when growing up. Generally, it starts from development and ends at production. There can be some more stages within this two ends. And, in maximum cases this stages are overlapped. The overlapped stages can be hosted in different servers with different settings but shares the same code. In this situations, if we want to keep bootstrap in SVN repo, it needs to setup bootstrap in a little different way. So that, it can handle many server settings with a single bootstrap.

For example, just now I am working on an application which is being developed in 3 local machines (me and two other members of my team) and being tested in our staging server. Besides, our client is monitoring progress of current sprint in their staging server and testing completed sprints in their pre-production server. Here, except the production servers, all hosts contains a checked out copy of code from a single repository. But this hosts have some difference in their library path, database host and authentication etc.

If my assumptions are ok for you, let’s start after downloading.


I have assumed that you are familiar with Zend Framework. And already worked on (atleast one) ZF MVC application. You know the basic things about bootstrapping and using the most common directory structure (shown in image). However, you can tune this technique for your favorite Zend directory stracture.

Download files

Downoad the files from here. The zip file contains –

  1. index.php – This is the bootstrap file. Your .htaccess file points all request to this file.
  2. config.xml – Contains all configurations of your application, for all hosts.
  3. Bootstrap.php – A php class that handles all startup settings in a vary organized way. (I don’t know who first wrote it, Thanks to him)

Continue reading

Image manipulation in Zend Framework using PHP Thumbnailer Class v2.0

In the huge set of library, Zend Framework have no options for manipulating image. There is a proposal for Zend_Image which aimed to provides easier image manipulation using GD. But the proposal was not considered. Bill Karwin of ZF said,

Proposal not being considered

This proposal has been reviewed. The proposal describes functionality that is very similar to the existing PHP interfaces for ImageMagick or GD.

We are not considering this for inclusion in the Zend Framework at this time.

So, what to do when we need to make image thumbnail, resizeing or cropping image etc in Zend Framework?

The Solution

Zend Framework has the extensive power of integrating any library. So, we can use ImageMagick or GD library or any wrapper class using this libraries for manipulating image.

Here, I am explaining creating image thumbnail using PHP Thumbnailer Class v2.0 in 2 easy steps. I am assuming that, you are using Zend’s MVC with common directory structure and you’ve GD library with your PHP installation.

Step 1 – Create a Model for image manipulation :

Download the PHP5 version of PHP Thumbnailer Class v2.0 and put the class file “” in your model directory. Now change the file name to “Thumbnail.php” as it’s class name. Now your model is ready for action.

Step 2 – Use this model in Controller :

In your Controller, write a function to make thumbnails using this model. For example, you can use this function below:

<span style="color: #606060">   1:</span> <span style="color: #008000">/**</span>
<span style="color: #606060">   2:</span> <span style="color: #008000"> * Create thumbnail of an image</span>
<span style="color: #606060">   3:</span> <span style="color: #008000"> * </span>
<span style="color: #606060">   4:</span> <span style="color: #008000"> * @author    Anis uddin Ahmad (</span>
<span style="color: #606060">   5:</span> <span style="color: #008000"> * @param     string    Path of source image</span>
<span style="color: #606060">   6:</span> <span style="color: #008000"> * @param     string    Destination path of thumbnail</span>
<span style="color: #606060">   7:</span> <span style="color: #008000"> * @param     number    Width of thumbnail</span>
<span style="color: #606060">   8:</span> <span style="color: #008000"> * @param     number    Height of thumbnail</span>
<span style="color: #606060">   9:</span> <span style="color: #008000"> * @param     number    (optional) thumbnail image quality </span>
<span style="color: #606060">  10:</span> <span style="color: #008000"> */</span>
<span style="color: #606060">  11:</span>
<span style="color: #0000ff">private</span> <span style="color: #0000ff">function</span> _createThumbnail($sourcePath, $destPath, $w, $h, $q = 100)
<span style="color: #606060">  12:</span>  {
<span style="color: #606060">  13:</span>     $thumb = <span style="color: #0000ff">new</span> Thumbnail($sourcePath);
<span style="color: #606060">  14:</span>     $thumb-&gt;resize($w, $h);
<span style="color: #606060">  15:</span>     $thumb-&gt;save($destPath, $q);
<span style="color: #606060">  16:</span>  }

Done. Now you can use this function to resize/make thumbnail of an image. To resize an image with keeping it’s original name, pass the $sourcePath again in $destPath parameter. Then it will overwrite the existing image.

So, was it so though to manipulate image in ZF?

Join Problems with Zend_Paginator and Zend_Db_Select objects

Zend_Paginator is one of the newest members of Zend framework family. It’s surely a rich addition with Zend’s power. Moreover, It’s handling the main data of pagination, not only pagination links. Which I didn’t found in any other pagination library.

But, when I (and many other developers) start to working with this library, found many problems/bugs. The matter is, they are being solved very fast and hope to get a bullet-proof pagination library soon. For example, here is tow important fixings about Zend_Paginator and Zend_Db_Table_Select.

I had to face a problem with Zend_Paginator when using Join in Zend_Db_Select object. I am explaining here how the problem arises and how to solve it.

The Problem

One of the 4 Adapters for Zend_Paginator is DbSelect which uses a Zend_Db_Select instance. Now, the problem occurred if I need to have some calculative data from other tables and use join with the Zend_Db_Select object for them. For example, I am retrieving data from `deals` table and collecting number of comments from `comments` table and total votes from `votes` table. So, with other conditions, I had to add this lines with select object:

<span style="color: #606060">   1:</span> $select-&gt;joinLeft(array(<span style="color: #006080">'c'</span> =&gt; <span style="color: #006080">'comments'</span>), <span style="color: #006080">' = c.deal_id'</span>, array(<span style="color: #006080">'total_comments'</span>=&gt; <span style="color: #006080">'count('</span>) );
<span style="color: #606060">   2:</span> $select-&gt;joinLeft(array(<span style="color: #006080">'v'</span> =&gt; <span style="color: #006080">'votes'</span>),    <span style="color: #006080">' = v.deal_id'</span>, array(<span style="color: #006080">'total_votes'</span>   =&gt; <span style="color: #006080">'count('</span>) );
<span style="color: #606060">   3:</span> $select-&gt;group($<span style="color: #0000ff">this</span>-&gt;_name . <span style="color: #006080">'.id'</span>);


Now, the Zend_Paginator will always calculate the TotalRows as 1. That means, $this->totalItemCount; will always print 1 in “view partial“.

Why it happens?

Let’s take a look to the count function of DbSelect Adaptor of Zend_Paginator:

<span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> count()
<span style="color: #606060">   2:</span> {
<span style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> ($<span style="color: #0000ff">this</span>-&gt;_rowCount === <span style="color: #0000ff">null</span>) {
<span style="color: #606060">   4:</span>         $expression = <span style="color: #0000ff">new</span> Zend_Db_Expr(<span style="color: #006080">'COUNT(*) AS '</span> . self::ROW_COUNT_COLUMN);
<span style="color: #606060">   5:</span>         
<span style="color: #606060">   6:</span>         $rowCount   = clone $<span style="color: #0000ff">this</span>-&gt;_select;
<span style="color: #606060">   7:</span>         $rowCount-&gt;reset(Zend_Db_Select::COLUMNS)
<span style="color: #606060">   8:</span>                  -&gt;reset(Zend_Db_Select::ORDER)
<span style="color: #606060">   9:</span>                  -&gt;reset(Zend_Db_Select::LIMIT_OFFSET)
<span style="color: #606060">  10:</span>                  -&gt;columns($expression);
<span style="color: #606060">  11:</span>                  
<span style="color: #606060">  12:</span>         $<span style="color: #0000ff">this</span>-&gt;setRowCount($rowCount);
<span style="color: #606060">  13:</span>     }
<span style="color: #606060">  14:</span>&nbsp; 
<span style="color: #606060">  15:</span>     <span style="color: #0000ff">return</span> $<span style="color: #0000ff">this</span>-&gt;_rowCount;
<span style="color: #606060">  16:</span> }

Here, at line 7, the column list is being removed. But we have a `Group By` clause in our select object which is not cleared by this function and the result is always 1. At this point, if we add “->reset(Zend_Db_Select::GROUP)” after line 9, the calculation will get rid from always being 1. But there will arise another problem. That is, joined tables are still remaining in `From` clause. You can see them by adding “die(print_r($rowCount->getPart(Zend_Db_Select::FROM)));” after line 10. This join tables will make the calculation inconsistence because there is no expression columns now to summarize their result.

The Solution

Just alter this function as following to prevent this problem:

<span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> count()
<span style="color: #606060">   2:</span> {
<span style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> ($<span style="color: #0000ff">this</span>-&gt;_rowCount === <span style="color: #0000ff">null</span>) {
<span style="color: #606060">   4:</span>         $expression = <span style="color: #0000ff">new</span> Zend_Db_Expr(<span style="color: #006080">'COUNT(*) AS '</span> . self::ROW_COUNT_COLUMN);
<span style="color: #606060">   5:</span>        
<span style="color: #606060">   6:</span>         $rowCount   = clone $<span style="color: #0000ff">this</span>-&gt;_select;
<span style="color: #606060">   7:</span>         $rowCount-&gt;reset(Zend_Db_Select::FROM);
<span style="color: #606060">   8:</span>         
<span style="color: #606060">   9:</span>         <span style="color: #0000ff">foreach</span>($<span style="color: #0000ff">this</span>-&gt;_select-&gt;getPart(Zend_Db_Select::FROM) <span style="color: #0000ff">as</span> $name =&gt; $table)
<span style="color: #606060">  10:</span>         {
<span style="color: #606060">  11:</span>             <span style="color: #0000ff">if</span>(empty($table[<span style="color: #006080">'joinCondition'</span>]))        
<span style="color: #606060">  12:</span>             {
<span style="color: #606060">  13:</span>                 $rowCount-&gt;from(array($name =&gt; $table[<span style="color: #006080">'tableName'</span>]));
<span style="color: #606060">  14:</span>             }
<span style="color: #606060">  15:</span>             elseif($table[<span style="color: #006080">'joinType'</span>] == <span style="color: #006080">'inner join'</span>)        
<span style="color: #606060">  16:</span>
<span style="color: #606060">  17:</span>                 $rowCount-&gt;join(array($name =&gt; $table[<span style="color: #006080">'tableName'</span>]), $table[<span style="color: #006080">'joinCondition'</span>]);
<span style="color: #606060">  18:</span>             }
<span style="color: #606060">  19:</span>         }
<span style="color: #606060">  20:</span>         
<span style="color: #606060">  21:</span>         $rowCount-&gt;reset(Zend_Db_Select::COLUMNS)
<span style="color: #606060">  22:</span>                  -&gt;reset(Zend_Db_Select::ORDER)
<span style="color: #606060">  23:</span>                  -&gt;reset(Zend_Db_Select::GROUP)
<span style="color: #606060">  24:</span>                  -&gt;reset(Zend_Db_Select::LIMIT_OFFSET)
<span style="color: #606060">  25:</span>                  -&gt;columns($expression);
<span style="color: #606060">  26:</span>&nbsp; 
<span style="color: #606060">  27:</span>         $<span style="color: #0000ff">this</span>-&gt;setRowCount($rowCount);
<span style="color: #606060">  28:</span>     }
<span style="color: #606060">  29:</span>&nbsp; 
<span style="color: #606060">  30:</span>     <span style="color: #0000ff">return</span> $<span style="color: #0000ff">this</span>-&gt;_rowCount;
<span style="color: #606060">  31:</span> }

At line 9, I am running a foreach which will remove the `from` clause and again assign only the main table and inner join tables. And, at line 23, just removing the `Group By` clause from query. Now, this function will provide right calculation even when there is some joins like this. But, if you use inner join in your Zend_Db_Select object (which is very rare for pagination), this function may not serve properly.

If there is any mistake in this alternation or any other way can make it better, please let me know. And… let’s meet power with simplicity.

UPDATE [26/08/08] : The function is updated and hopefully it will work fine for inner joins too.

UPDATE [24/09/08] : This issue is solved in ZF 1.6. Jurrien Stutterheim has reported about this post in Zend Framework Issue tracker and worked on it. After the release of 1.6, he requested me to check if this issue still exist. I’ve checked and found it solved. Thanks to Jurriën Stutterheim.