The use of joins can never be ignored.
Zend Framework developers have done tremendous job by providing simple method for implementing joins.
Lets look some examples of different type of joins.
Before discussing joins lets consider we have two tables, “authors” and “books”.
These are associated with author_id.
1. Inner Join
The simplest query will be
$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinInner('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->order('column name ASC/DESC');
2. Left Join
$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinLeft('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');
3. Right Join
$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinRight('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');
4. Full Join
$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');
5. Cross Join
$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');
Once you write these queries, you can fetch a single row or multiple rows as
$result = $this->getAdapter()->fetchRow($select);
$result = $this->getAdapter()->fetchAll($select);
The first statement fetch only one row, while the second statement fetch the entire dataset.
Hi, can you tell me how can I use Join with my own model.?
ReplyDeletevery poor examples. It would have been very useful to know what object is this, then how to enumerate the result set :(:(:(
ReplyDeletehow to use AND operator in WHERE clause in zend framework.
ReplyDeleteex.
" SELECT * FROM users
WHERE `username`='prabha' AND `password`= 'change'";
Thank for the article. It helped me a lot.
ReplyDeleteBut I think, these "selects" are not perfect. 'books', 'authors' are tables name not the model object. So when a table names is changed, the table name has to be adapted in every file. The talbe name can be got from model object. But to read table name from model object a getName() method have to be implemented to read protected $_name.
Is there a offical way to solve this table name problem?
Ting
Thank for the article.
ReplyDeleteHow do I turn this:
ReplyDeleteselect advertisercontest.*, advertiseraccount.advertiserid, advertiseraccount.companyname from advertisercontest left join advertiseraccount on advertiseraccount.loginid = advertisercontest.loginid where advertisercontest.golive is not NULL;
into a left join in Zend..
Hi Prabhakar,
ReplyDeleteUse AND operator in WHERE clause:
$select = $this->_db->select()
->from('books')
->where('where 1st condition')
->where('where 2nd condition')
Nice examples.This is good when we are using only one model class but how we should write the same when we are using Zend mappers?
ReplyDeletewhat are zend mappers
DeleteReally guys, using this sort of abstracted interface to a relational database is for amateurs.
ReplyDeleteAnyone worth anything in programming should know how to construct a SQL query, it is well known we've been doing it for decades. Putting this sort of wrapper around SQL is nonsense. What happens is that you end up with multiple methods of writing queries:
Using Zend_Db to do simple CRUD on single tables (maybe one join OMG)
Using PDO when things get to complex for Zend_Db
and using native queries when you have to do anything useful.
Why bother ???
Using a Zend_Db type interface just clouds what is basically a simple interface.
You will end up trying to shoehorn your data access into an abstraction layer that gives no value. Compromising design and performance all over the place.
Don't be lazy, learn the native SQL and how to interface to it.
And if anyone says that it makes swapping the db vendor easier - there are many many other factors far more important that an abstracted Query interface.
on the joins you might need to use AS to teh fields if they are the same in both tables
ReplyDelete$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinLeft('authors','books.id=authors.bks_id',array('books.msg as msg1','authors.msg as msg1'…))
...
i use the inner join query but i am receiving error
ReplyDeleteNotice: Undefined property: UsersController::$_db in C:\xampp\htdocs\myzend\application\controllers\UsersController.php on line 259
Fatal error: Call to a member function select() on a non-object in C:\xampp\htdocs\myzend\application\controllers\UsersController.php
Please help me out , thanks.
Hello
ReplyDeleteAny one help me hot cache the project in zend
Any one can help me how we can cache a project in zend
ReplyDeleteThanks! Your tutorial solved my doubts
ReplyDeleteI like your blog which is pretty awesome because I found some good information on it. Basically I am searching regarding Offshore Software Outsourcing but found this blog which is good.
ReplyDeleteHi admin, i get data in model with db_table Zend_Db_Table_Abstract, how can i get data from two tables with common field
ReplyDeletev
ReplyDeleteThanks for the articles
ReplyDeleteIt contains very good information regarding Zend framework,The way you presented is very good and simple to understand.Thanks for sharing this information for our knowledge.
ReplyDeleteThank you.
oracle R12 training
Hi,
ReplyDeletegood information
Web application development services will successfully change the execution of your online business So, hire our skilled Ecommerce website company to get customized applications for your business.
Best Website Designing and Development Company
"
Ecommerce Website Development Company"
Zend Framework
Thanks for sharing this informative article on Zend Framework SQL Joins Examples. PHP has many framework and Zend is one of them. If you have any requirement to Hire Zend Developers or any PHP web developers for your project. Please visit our website.
ReplyDelete