cakePHP generateList for custom query
I had a situation today in a cakephp project where I needed to do a custom query to get at a bunch of user names that had some pretty ethereal associations with the view I was working on. That was fine, some SQL later I was getting at the correct data. The issue I now had as that whereas previously I would have just done…
-
-
$this->set(‘people’, $this->Thing->People->generateList());
-
then in the view I would have done…
I now couldn’t as there was no way to run generateList on my new custom query (if there is a way that you know I’d love to hear about it).
Anyway, after some digging around I was able to refactor some of the code in generateList() to do what I wanted…
-
-
$myPeople = $this->Thing->query("SELECT people.name, people.id FROM … custom SQL …");
-
-
// this is the cool stuff
-
-
$myPeople = array_combine($keys, $vals);
-
}
-
-
$this->set(‘people’, $myPeople);
-
Now you can use the OOTB select to generate a drop down that is populated from your custom SQL.
I have to say, I was pretty blown away by extract, never really used it before, but it’s pretty handy.
About this entry
You’re currently reading “cakePHP generateList for custom query,” an entry on Jon Sykes
- Published:
- 07.29.08 / 12am
- Category:
- Back End Code
QR Code for this Blog Post
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]