Recent Posts
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
- (09/27) Upgrading CakePHP Application to 1.3 (from 1.2)TAGS:CakephpUpgrade
Subscribe to my feed
MainelyDesign.com Blog
Forcing A Single Join in CakePHP Pagination
Posted on 10/14/2011 at 09:25 am by Kevin Wentworth
Viewed 1,029 times | 6 comments
The devil's in the details... I was trying to make a really simple, dreadfully easy, database join in my CakePHP web application. I've forced joins in Cake before, using the 'joins' key in the options array for find calls and paginate calls with no issue. It was late and for the first time I only wanted to use a single join. I copied the join code from a much more complex web app and pasted it into my new 'joins' conditions. And then... I got SQL errors.
That query doesn't look right...
Looking at the query I could see 2 things that weren't right:
- The table wasn't being aliased using the AS keyword, it was missing.
- The join was happening ON Array!
Joins have to be specified in an array
Even if there is a single Cakephp join! Remember this. It looks weird, but if you don't encapsulate your join in an array, your SQL query will look even weirder.
The right way to use 'joins' options in CakePHP
- $this->paginate['User']['joins'] =
- 'table' => 'some_join_table',
- 'alias' => 'SomeJoin',
- 'type' => 'inner',
- 'conditions' =>
- 'SomeJoin.ref_id = Ref.id'
- )
- )
- );
Notice the 'joins' => array(array());
The wrong way
- $this->paginate['User']['joins'] =
- 'table' => 'some_join_table',
- 'alias' => 'SomeJoin',
- 'type' => 'inner',
- 'conditions' =>
- 'SomeJoin.ref_id = Ref.id'
- )
- )
Notice only 'joins' => array(); is specified. This is not the right way- you need to use 'joins' => array(array()); as above.
Hopefully, this helps someone. If nothing else, I won't forget about this little feature!
Cheers,
-Kevin Wentworth
Tags for Forcing A Single Join in CakePHP Pagination
Cakephp | Cakephp 13 | Database | Errors | Habtm | Mysql | Mssql | Web Programming | Usage
Comments for this Posting
Posted by fitnessfitness
on 7/12/11
Usually, fashion, considered as fads, is extremely changeable. Then, what is actually the fashion trend?
In fact, your closet, for example, can answer it. When it comes to the fashion industry, it's a continuously changing world. Even though some
trend
Posted by fitnessfitness
on 7/12/11
Resources like the one you talked about right here shall be very helpful to me! I'll post a link to this web page on my blog. I am sure my guests will find that very useful.
Posted by affordableweddi
on 26/12/11
This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as
Posted by affordableweddi
on 26/12/11
I'm still learning from you, while I'm making my way to the top as well. I certainly enjoy reading all that is written on your blog.Keep the posts coming. I enjoyed it!
Posted by lrrushlowflet
1 day, 23 hours ago
pre owned handbags, used louis vuitton handbags, used chanel handbags, used ... used designer handbags, used handbags, used designer purses for sale.

Posted by fitnessfitness
on 7/12/11
Awesome site you've got here. Looks like you really know your stuff. I'm glad I found this in my searches.I realy liked your angle that you have on the topic. Certainly wasn't planning on this at the time I begun browsing for tips. Your ideas were totally
www.uggbootsonlinecheap.com