4
Mar
Posted by Chris Hartjes in CakePHP. 7 Comments
(Note: I will be cross-posting a version of this to the CakePHP Cookbook within a day or two).
As amazing as the built-in CakePHP pagination helper is, sometimes you have to create your own custom queries for data you wish to paginate. For the simulation baseball league site I wanted to be create an admin area where I could paginate through the series instead of individual games. It took some digging around with google, but I found some info in a thread (thanks to Baz for contributing that tidbit to the thread) on how to do just that.
Okay, so it turns out that the paginate() method that is used to generate the data that you (oddly enough) paginate through takes the same arguments as Model::findAll(). So, if you want to use your own query, you simply create a 'paginate' method for your model. In my case, I needed a query that would group things together (because a 'series' is simply a collection of all games between two teams in a particular week). The query stuff I did is ugly because there currently is no support 'group by' in CakePHP (although that would be a very interesting project to tackle). So, here's what I did:
PHP:
-
/**
-
* Custom paginate method
-
*/
-
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null) {
-
$conditions[] ="1 = 1 GROUP BY week, away_team_id, home_team_id";
-
$recursive = -1;
-
$fields =
array('week',
'away_team_id',
'home_team_id');
-
-
return $this->findAll($conditions, $fields, $order, $limit, $page, $recursive);
-
}
So, that's the first part of the custom query stuff taken care off. Next, I needed to be able to properly count the number of 'series' so that the numbers (you know, that stuff that says 'page 7 of 23') come out properly. Again, you can override the paginateCount() method if you need to. It uses the same parameters as Model::findCount(). Now, the custom query I'm using below is Postgres-specific, so YMMV:
PHP:
-
/**
-
* Custom paginateCount method
-
*/
-
function paginateCount($conditions = null, $recursive = 0) {
-
$sql = "SELECT DISTINCT ON(week, home_team_id, away_team_id) week, home_team_id, away_team_id FROM games";
-
$this->recursive = $recursive;
-
$results = $this->query($sql);
-
-
-
}
So there you have it. Hope this helps out anyone who's been trying to figure out how to use custom queries with their pagination.
Article Tags >>
CakePHP ||
pagination
18
Feb
Posted by Chris Hartjes in CakePHP. 3 Comments
Of all the people in the CakePHP community that I have come across, none suffers from a lack of respect more than John David Anderson, more commonly known as _psychic_ on IRC. Why do I say this? John heads the CakePHP documentation project, a thankless task that seems only to exist so unimaginative people can complain that there is no documentation for CakePHP. If I had a dollar for every person on the CakePHP mailing list who seems to either not understand how to use Google or how to look at someone else's PHP code to figure out how something works, I could retire and blog full time.
But I digress. With a ton of help from Andy Dawson (AD7Six) the documentation for CakePHP 1.2 has moved forward immensely with the unveiling of the CakePHP Cookbook. It's a combination of manual and wiki, where people can flesh out the manual and make comments on the methods in there. I have been quite harsh on people who have called for a CakePHP wiki in the past, because they tend to be a mess. This project is just different enough that I cannot stop recommending people to use the cookbook when searching for answers to their CakePHP problems. In fact, if you can contribute by moving entries over from the temporary documentation to the cookbook, _psychic_ will have one less thing to do.
From now on, whenever I find a solution to something in CakePHP I will not only post it here on my blog (because, after all, I am still an egomaniac) I will also create an entry in the Cookbook if it's appropriate. Lucky for you, contributions are anonymous so you won't be able to tell what I've put in there. My first contribution has been to get my documentation on the Auth component in there (although my simple user registration stuff isn't in there), and Baz added his stuff he's been doing with Auth as well.
So, no more excuses about lack of documentation! Anyone who registers and account can create entries in the manual, and everyone who complains about on the mailing list will be told to go look at the cookbook and contribute changes and comments to it.
Article Tags >>
CakePHP ||
Cookbook
12
Feb
Posted by Chris Hartjes in CakePHP. No Comments
Fresh off the middling success of my talk at CakeFest 2008, I'll be giving a more expanded version of the talk at Open Web Vancouver 2008, which is being held April 14 and 15, 2008 in Vancouver. I really enjoyed going out there last year, and this talk is quite different from the last one I gave. However, I will have to flesh this one out as I will be talking to a crowd that might not necessarily be familiar with CakePHP, or even MVC frameworks as a whole. The cute slides done by my kids are definitely going to stay in it.
Article Tags >>
CakePHP ||
Open Web Vancouver 2008
2
Feb
Posted by Chris Hartjes in CakePHP. No Comments
I had been struggling with some weirdness that CakePHP has been displaying while trying to add some new features to the baseball league website, where Cake + PHP 4.4 + Postgres were deciding to just be sulky and not work together well.
So, I was trying to figure out how to make a test for this so I could file a bug (CakePHP not finding the proper name of the Postgres sequence for a table) when I tracked down nate on IM to whine to him about this problem, he started bugging me about "not paying attention" when looking around in code to try and track down what I thought was a bug. If I had looked at the API a little closer I would've been steered towards the solution he gave me.
Lucky for me, it turns out that the solution (for now) to the problem was to simply add the name of the sequence for that table as a variable to the model definition.
PHP:
-
<?php
-
class Vote extends AppModel {
-
-
var $name = 'Vote';
-
var $sequence = 'votes_id_seq'; // This is what I added in
-
-
//The Associations below have been created with all possible keys, those that are not needed can be removed
-
-
'BallotItem' =>
array('className' =>
'BallotItem',
-
'foreignKey' => 'ballot_item_id',
-
'conditions' => '',
-
'fields' => '',
-
'order' => ''
-
),
-
'Franchise' =>
array('className' =>
'Franchise',
-
'foreignKey' => 'franchise_id',
-
'conditions' => '',
-
'fields' => '',
-
'order' => ''
-
)
-
);
-
var $validate =
array('ballot_item_id' =>
array('rule' =>
'numeric',
-
'required' => true),
-
'franchise_id' =>
array('rule' =>
'numeric',
-
'required' => true),
-
'answer' =>
array('rule' =>
'numeric',
-
'required' => true,
-
'message' => 'You must vote on this item')
-
);
-
-
}
-
?>
This confirmed to me that my new-found respect for trying to come up with the simplest solution to a problem is the correct way to do so. Since my head is full of all sorts of nonsense (work items, family life, baseball stats, old Dungeons & Dragons adventures from my teenage years) I find that at times I am better served by taking the time to think about the problem rather than start digging around. Especially when it comes to code that was well-written an has been tested by lots of people, like the overwhelming majority of CakePHP's core code.
Thanks again to nate for proving to me that the Devil's in the details. And continuing to make me feel stupid.
Article Tags >>
CakePHP ||
Postgres
21
Jan
Posted by Chris Hartjes in CakePHP. 2 Comments
Yes, it's been confirmed: I will be attending CakeFest down in Orlando Feb. 6 to 8. I'll be giving a talk entitled "Fake It Until You Make It" about how to use the Cake console tools to speed up your development. I use the Cake console for all my CakePHP projects, since it helps me quickly create the code for models, controllers (love being able to have all those admin methods already baked in) and I hope to show those who might be a little shy of using the command line what they are missing out on.
I'm going to be flying down Tuesday night, getting in around midnight and flying out Friday morning (it's looking like a 10:30 departure). So that's two full days at the conference, which is okay. Now, if anyone is going down there and could use a roomie to help cut down on costs let me know.
Article Tags >>
CakeFest ||
CakePHP
Recent Comments