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
11
Dec
Posted by Chris Hartjes in CakePHP. 3 Comments
There are usually lots of Has And Belongs To Many relationship questions on the CakePHP mailing list. Since I am stupid about this stuff, I sought out Nate Abele and bugged him via IM until he agreed to give me an example of how to do this. Thanks Nate!
PHP:
-
class Tag extends AppModel {
-
-
var $hasAndBelongsToMany = 'Post';
-
-
function paginate($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
-
$tag = $conditions['tag'];
-
unset($conditions['tag']);
-
-
$this->hasAndBelongsToMany['Post'] = am(
-
$this->hasAndBelongsToMany['Post'],
-
compact('conditions',
'fields',
'order',
'limit',
'page')
-
);
-
return $this->findByName($tag);
-
}
-
-
function paginateCount($conditions = null) {
-
$tag = $conditions['tag'];
-
unset($conditions['tag']);
-
$tmp = $this->hasAndBelongsToMany['Post'];
-
$this->
hasAndBelongsToMany['Post']['fields'] =
array('id');
-
$tag = $this->findByName($tag);
-
$this->hasAndBelongsToMany['Post'] = $tmp;
-
return count($tag['Post']);
-
}
-
}
-
-
?>
-
-
<?
-
// Controller code
-
-
$data =
$this->
paginate('Tag',
array('tag' =>
$tag));
-
// Where $tag = some tag name
Okay, that's great but WHY does it work? Normally, when you do pagination you have to pass it a bunch of different parameters so it knows what records you wish to include as part of the data set you want to paginate over. In this example, you've already established that Tag HABTM Post. In order for the pagination query to correctly pull out the Posts that are also associated with the Tag, you need to merge the parameters you passed in to your 'paginate' method so that the CakePHP data mapping functionality knows what associated records are to be included. Then run your $this->findByName(...) and you're all set.
Article Tags >>
CakePHP ||
HABTM ||
pagination
Recent Comments