Posted by Faheem Abbas on November 26, 2008
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)
{
parent::__construct($options);
$this->setMethod(‘post’);
$this->setAttrib(‘enctype’, ‘multipart/form-data’);
$file1= $this->createElement(‘file’,'file1′);
$file1->setLabel(‘File1:’);
$file2= $this->createElement(‘file’,'file2′);
$file2->setLabel(‘File2:’)
->addValidator(’size’,'10′);
$submit=$this->createElement(’submit’,’submit’);
$this->addElements(array(
$file1,
$file2,
$submit
));
}
}
?>
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($this->getRequest()->isPost()){
if($form->isValid($_POST)) {
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator(‘Size’, false, 20000, ‘file2′);
$upload->setDestination(‘tmp/’);
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {
if($upload->isValid($file)){
$upload->receive($file);
}
}
}
}
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.
Posted in Zend Framework | 4 Comments »
Posted by Faheem Abbas on November 26, 2008
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()
->from(array(‘bks’=>’books’),array(‘id’,‘book_name’,’book_pub_date’))
->join(array(‘auth’=>’authors’),’bks.id=auth.book_id’,array(‘id’,’author_name’));
->where(‘bks.id=?’,2);
$results = $this->getAdapter()->fetchAll($select);
Zend_Debug::dump($results);
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
->join(array(‘auth’=>’authors’),’bks.id=auth.book_id’,array(‘id’,’author_name’));
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()
->from(array(‘bks’=>’books’),array(‘id’,‘book_name’,’book_pub_date’))
->joinLeft(array(‘auth’=>’authors’),’bks.id=auth.book_id’,array(‘id’,’author_name’));
->where(‘bks.id=?’,2);
$results = $this->getAdapter()->fetchAll($select);
Zend_Debug::dump($results);
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.
Posted in Zend Framework | 2 Comments »