Archive | November, 2008

Zend Framework getting table meta data infromation

27 Nov

Yesterday I was checking how can we get table meta data information, like table name, columns names etc, and was surprised to see that how easy It is to have these information using Zend_Db component of Zend Framework.
Let us see how we can get table name and columns names using a simple example.
Consider you have a table with name “company” with following information.

• Id
• Company_name
• Address
• Phone

First create a model class as

class Company extends Zend_Db_Table
protected $_name= “company”;


Next in your controller/action.
Create an instance of your newly created class as

$company = new Company();

And now get meta data information as

$info = $company->info();

This will return an array of information.
You can check the information return by simply writing


The will show the entire information return by info().
If you want to have column names simple write

foreach($info[“cols”] as $col) {
echo $col . “\t”;


This will show the name of the colums of your table.
That’s it. Enjoy.


Uploading multiple files using Zend framework- revised

26 Nov

In my previous post I discuss, how to upload multiple files. Sorry for that because I didn’t check the method. And I haven’t tested that on my system and don’t know whether it will be working or not. However All other example I provided in my articles are tested and working fine.
To find an appropriate way of uploading file, I have done it with other method. This is quite helpful and easy.
Fisrt create your form as

class forms_SimpleForm extends Zend_Dojo_Form
public function __construct($options = null)
$this->setAttrib(‘enctype’, ‘multipart/form-data’);

$file1= $this->createElement(‘file’,’file1′);

$file2= $this->createElement(‘file’,’file2′);



The code above is very simple.
We are first creating two file fields and a submit button and then add It to the form using addElements method.
Next in the controller/Action wirte code as

if($form->isValid($_POST)) {
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator(‘Size’, false, 20000, ‘file2’);
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {

In the above code we first check whether form is posted and valid. if yes then
We create adapter. Add validator to the file2 and set destination directory.
Next we get info by using $files = $ adapter ->getFileInfo(); using foreach loop and check each file. If file is valid then we upload it other wise leave ignore it.
You can even show error messages if you like. However I am not going to descuss how to handle and show error messages.

Zend Framework Selecting data from multiple tables using joinsr

26 Nov

When I was working in simple php, it was real pain in ass to write queries, especially complex queries like using joins and sub queries.
Zend has done fabulous job by implementing and providing simple methods for fetching data from multiple tables
Let us take a simple example and feel the power of Zend.
Consider we have two tables “books” and “authors”.
Books has the following fields

  • id
  • book_name
  • book_pub_date

and author table has

  • id
  • author_name
  • author_address
  • book_id(FK)

To select data from these tables using simple join, write the following code

$select = $this->_db->select()




$results = $this->getAdapter()->fetchAll($select);



In the above code we first make our select statement.
In the form table we have defined two arrays. array(‘bks’=>’books’) define alias for table books and array(‘id’,‘book_name’,’book_pub_date’) define which columns of the books table we want to fetch.
Next we define join as


The first array define alias for books. The second parameter is condition(on condition that is used in joining table) and the next array define the columns we want to fetch from the second table. Next line define the where clause. We then fetch records and dump it using Zend_Debug::dump($results) to see results that are returned.
The above example was for simple join, sometime you need to use left or right join. So its is as easy as the above example.
To write a left join, consider the following code.

$select = $this->_db->select()




$results = $this->getAdapter()->fetchAll($select);


The code above is similar to the first example, we have only changed join to joinLeft. And that’s it your left join. Similarly you use other joins.
Feel free to post comments.

Zend Framework Zend_Db working examples

25 Nov

In my first web application when I used Zend, I didn’t write a single line of code in my models.
I’d only created models directory when I was doing necessary configuration. That was because I wasn’t aware what MVC is all about and was individually working on the application.
After switching to one of the best organization and finding a place in a team that was and is working on a large scale web application in Zend Framework, I really feel the test of MVC.
As I have now worked on Zend for about 7 months, I know where to put necessary code. The role of models in creating large scale application using any framework can never be ignored.
It will be a big mistake if developer place code related to business logic into controller instead of model.
I will recommend the following.

Make necessary configuration in your bootfile. If you want to connect to the database, write following code in your boot file.


‘database’ => array(

‘adapter’ => ‘Pdo_Mysql’,

‘params’ => array(

‘host’ => ‘localhost’,

‘username’ => ‘root’,

‘password’ => ‘’,

‘dbname’ => ‘database_name’




$config=new Zend_Config($configData);

That’s it. You have now configured your application and can extend your model class from Zend_Db_Table, and all the functionality define in Zend_Db_Table will automatically be available to your class.

Now If you want to fetch, insert, delete data from database. Create a file in your model and put all the functionality in that file.
It will be better if you create separate model file-file in your model directory, for each table in your database.
If you have a table name users in your database, simply create users.php in your application/models directory and write the following code.


class Users extends Zend_Db_Table


protected name=”users”;



This is your model class. You can now perform different functions like insert, update, delete on the user table with writing only few lines of code.
In your controller/action write

$users = new Users();

This two lines of code will now insert new row into your user table.


This line will update the row having id=2 with the specified values.


Will delete row from the users table with id=2;
All the above method become available to you when you extend your class from Zen_Db_Table.

While working in web applications you may need to fetch customize data from the database, so write function in your model classes instead of writing code in the controller and simply call your function.
To fetch data I will write in my model class, the following code

public function getUserData()


$select = $this->_db->select()



$results = $this->getAdapter()->fetchRow($select);

return $results;



And then in my controller I will write

$users = new Users();
$userData = $users->getUserData();

This will return row as an array.
I can dump it as


uploading multiple files using Zend framework file component with example

24 Nov

Some developer may find it hard to upload several files using Zend Framework file component. In some cases developers may provide more than one file fields, say five(5) and when user tries to upload three files, so instead of uploading those three files, his files are simply ignored from being uploaded.
This is because Zend Framework uses receive function which first check all the file fields. If any of them is empty or unvalid, Zend Framwork file receive() function stop execution and none of the file uploaded.
A simple solution to this problem is to take file names of each file element and pass it to the receive function like
Note that receive function take a path to the file in form of string.
Consider you have the following file fields in your form
file1,file2,$file3 etc.
so in your controller/action


And so on, the above code will give you paths of each file field. Now


So these three files will be uploaded to the specified directory.

Zend Framework Form file component

24 Nov

Zend Framework 1.6 ship file component for uploading file. This component was not available in version 1.5.
Although it has some issues in version 1.6, apologize that I haven’t used this component in version 1.7. So those who are working may find it more mature then version 1.6.
To better illustrate how this component is working lets take a simple example.
The first thing you would need to use this component will definitely be to create

So its very easy to do this in Zend framework, write the following code where you are creating your form.





That’s it, now when you Zend render your form, you will clearly see file field in your form. However don’t forget adding it to your form as


Now the real job start now. You will need to handle your uploading in your controller.
Place the following code in your ControllerAction.

$adapter=new Zend_File_Transfer_Adapter_Http();


'minwidth' => 0,
'maxwidth' => 450,
'minheight' => 0,
'maxheight' => 80)




// code to show errors


If you look at the code above and break it, I think it is simple to understand.
We are first creating instance of the only adapter available in version 1.6. line

$adapter=new Zend_File_Transfer_Adapter_Http();

Create an instance of the http adapter for uploading the file.
Next, we are applying validator to the file. We have define “ImageSize” validator to check the image size. Different type of validators are available and you can easily use them in the way we have used it. See Zend Framework documentation for file valiadors.
The next few line are real magic of zend.


The line above set the destination folder where the file(s) will be uploaded and


is magic method of file component that upload the file(s) and return either true on successful uploading or false on failure.
There are some issues when you want to upload multiple files using this method.

Zend Framework and dojo-plugin by name dojo not found

19 Nov

Some Zend developers are facing the problem of “plugin by name dojo not found”. As I have already worked in dojo and come across several issues, I think better to update you.

This problems can be because of two things, I think
1. You don’t have downloaded latest and stable version of the Zend Framework.
2. Your configuration of dojo with Zend is not upto the point.

If have old version say ZF 1.5 or 1.6, visit Zend Framework and download latest version ZF 1.7. Although dojo was available in Zend Framework version 1.6, however you may face some problems. The most important one is “plugin by name editor not found.”. This is because Zend Framework 1.6 don’t have plugin class for dojo editor. However Zend Framework version 1.7 has this plugin. So its better to have latest but stable version of Zend.
Howeve if you have latest version and still unable to use dojo and having a bug “plugin by name dojo not found”. Read my post ““.
This will help you out.
Feel free to post questions.