| Paginate with hasMany relation, table not found? |
|
|
|
| CakePHP - Basics | ||
| Written by Stuart Duncan | ||
| Friday, 27 November 2009 15:12 | ||
|
Sometimes the thing that frustrates me most in programming isn't that I can't solve a problem, it's that I can solve it many different ways but not THE way that I know it should be done. What I mean is, CakePHP offers you all these ways of doing things but honestly, if you want your code to be efficient, reusable and most importantly, easy to understand for the next programmer... you have to do things the CakePHP way. And so my most recent issue of displaying a paginated list of results based on conditions that meet a second model (table) that had a 'hasMany' relationship really ate at my being because I just knew there had to be something I was missing, but nothing I could find anywhere gave me the answer. Hopefully this will help you... I have to mention, I am no pro and I write these articles as I learn so they might not be "the best" way of accomplishing the tasks, but I do ensure that they are within the scope of CakePHP and will be easily picked up on by any other CakePHP developer that looks at your code. Ok, here was my problem... I have an Actor model and these actors can have many different aliases (stage names), so in the Actor model, I create a $hasMany relationship and in the Alias table, I create a $belongsTo relationship. Pretty straight forward, the cake bake tool will even do this for you pretty quickly. The problem came into play when I created a search form where I could allow the user to find an actor based on their alias. Here's what I had:
There was more to it than that, but you get the basic idea. I was passing in a condition to have the alias name equal what was entered into the search box. This would cause CakePHP to get very mad at me and scream out in red letters "Alias table not found". I don't unerstand, I put into the models how they're related... how can it not be found? So I added in 'recursive' => 1 which didn't work.. 2... 3... nothing would get it to grab that Alias table. So I tried fiddling with the foreignKeys, the primaryKeys... the contains feature... Finally I found one single solitary hard to find article which was linked from another article which was linked from a post in a forum which I found in Google... and it partially answered my question enough for me to finally have a working solution. Here's what I ended up with:
I said partially, because the article I found only informed me of the join paramater... which I would have NEVER figured out on my own... I mean, an array immediately within an array?? The part I figured out, not that it takes a genius, is to add the group parameter because otherwise you will get a listing for each actor for each alias... so if actor 1 had 3 aliases, it would list actor 1 three times. That's no good, so grouping it makes it show actor 1 only one time. Anyway, there you have it... you can add more joins on as you wish, just throw a comma after that array within the array and list another table, alias, type and conditions and it'll just get joined onto the pagniation complete with search results. |




