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:

  1. The table wasn't being aliased using the AS keyword, it was missing.
  2. 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

  1. $this->paginate['User']['joins'] =
  2.     array(
  3.         array(
  4.             'table' => 'some_join_table',
  5.             'alias' => 'SomeJoin',
  6.             'type' => 'inner',
  7.             'conditions' =>
  8.             array(
  9.                 'SomeJoin.ref_id = Ref.id'
  10.             )
  11.         )
  12.     );

Notice the 'joins' => array(array());

The wrong way

  1. $this->paginate['User']['joins'] =
  2.     array(
  3.         'table' => 'some_join_table',
  4.         'alias' => 'SomeJoin',
  5.         'type' => 'inner',
  6.         'conditions' =>
  7.         array(
  8.             'SomeJoin.ref_id = Ref.id'
  9.         )
  10.     )

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

Bookmark and Share

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

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

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

www.wigshere.com

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.

www.uggbootsever.com

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

www.uggbootsever.com

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!

www.discountuggclearance.com

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.

www.kisslouisvuitton.com

Leave a Comment

Post a Reply

Will not be posted or used in any way.

We'll link back to your site if you want.

No links or formatting allowed.

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design