Free CakePHP E-Book - Super Awesome Advanced CakePHP Tips
For those of you who just want the book
This is the download link ---> Super Awesome Advanced CakePHP Tips
Updated to V1.1, fixing a few small things.
For those that want a 1500 word blog post
I've been sitting on this thing for the last two weeks and figure it's about time I just let it out. Ahhhh....so much better. Now, about the book. What better way to introduce it then a fake interview? Off we go!
What exactly is this thing and why should I waste my precious bowl time reading it?
At the moment there is no lack of CakePHP books geared towards beginners. But, there really isn't much for anyone who has mastered the basics and wants to do more. This e-book is for you guys and gal (I'm sure there's at least one out there somewhere).
I suck at CakePHP. What's the best way to learn the basics so I can get up to speed and read your book?
Check out Chapter 2: "How to Learn CakePHP". It details two paths for learning the framework - a free one and a pay one. I'm interested in feedback here, since it's hard for me to remember what it's like learn the framework. I just remember being really frustrated at times and blacking out, only to awaken covered in blood and standing over a dead hobo. Not good times.
How much is this thing going to cost me?
Nothing. It's free.
No really.
Seriously. Free. You know how when you're watching an independent movie or playing an indy video game you cut it some extra slack and overlook a few minor flaws? Do that there too. Thanks.
When did you start working on this?
The original idea was hatched in December 2007. At the time I was planning on writing a series of Advanved CakePHP posts. After I had 12-15 or so I was going to roll them into an e-book. Naturally I fizzled out after 2 posts.
Fast Forward to Oct 08 and I'm sent a copy of CakePHP Application Development to review. I realized as I was reading it (and confirmed by one of the comments on that post) that there was a lack of upper level Cake books/documentation. I also realized that in the year since I'd given up on the advanced series I had a bunch of decent posts that, once I cleaned out all the cursing and porn references, would make a decent e-book.
It's kind of dick of you to spend all this time creating an e-book, but then hardly contribute to the Cookbook. Do you feel any remorse or are you so hell bent on personal glory that it has crushed all rational thought in your oddly shaped head?
I struggled with this - I really did. In my defense I would say most of the content of the book isn't really appropriate for the Cookbook. That still doesn't excuse why I haven't contributed more. Although the Cookbook is great there are gaps (though they're becoming harder to find). I don't know...maybe it's because I spend more time in the API or maybe I'm still bitter that tempdocs, which served me so well, was whacked without even given a chance to make a tearful deathbed speech.
What IDE did you use to create the book?
I actually went through two before settling on Open Office. I started with Google Docs, because I figured it would be cool to be able to write whenever/wherever I got inspired. By about 30 pages Google Docs started to really choke, plus the PDF generation was mediocre at best.
I moved the whole thing to Word and spent like a week re-doing all the styles. But Word just gave me other problems. Whole pages would disappear until I scrolled around a bit and then they'd randomly re-appear. Plus I tried a bunch of different PDF printers and they all sucked.
Finally, I moved to Open Office and wasted another week fixing the styles again. This time everything worked pretty well and the built in PDF generator actually did a decent job.
Is this what you've been busy with and why you haven't blogged as much and when you do blog the posts blow?
Yea, pretty much. I'm also obsessed with converting all my open source Cake code into plugins. I'm all over plugins like a meathead frat dude trying to impress his buddies as a way to cope with his feelings of inadequacy when he sees a slightly unattractive chick with self esteem issues whose had too much to drink.
What's with the Mark Story sections?
This one is going to take a bit. Way back in the pre-RC days of 1.2 if you used the console to bake model tests the resulting test cases would have to extend the model and set the database to use the test_suite. This approached sucked and there was a better way. If you used the ClassRegistry to get an instance of the model it would set the database automatically. When I was putting the book together I knew I wanted to include a section of fixing the model unit test.
I also had an inkling I wanted to do a proper view unit test section. It would have been a gaping goatse-esq hole if I had a section of M(odels) and V(iews) and not C(ontrollers). Before I could get my shit together and write it Mark's article came out. Fuck. It was good and I really hated the thought of re-writing the same thing.
So, I got the brilliant idea to get others to contribute sections. I emailed Mark and a bunch of other Cake devs to see if they were interested. Predictably, everyone was too busy raking in that phat CakePHP bank to contribute. Except for Mark who not only agreed to let me use that piece, but also his forthcoming (at the time), landmark, life altering, spiritually awakening piece on mock objects. *genuflect*
Why not charge $$$ for this thing?
Another long answer. Continuing the above section...sometime later I realized that Cake had actually fixed the core to generate better tests and my Model section was pretty much shot. But at this point I already had Mark on board, so to sell this thing I would either have to a)cut his part and lose like 1/3 of the book or b) negotiate some sort of split.
Realistically I knew this thing wasn't going to make a ton of loot. Certainly not enough add another wing to the PseudoCoder estate. Take a look at this post from John Resig. It's a little unclear how much he's made on that book, since he never reveals how much he received up front $7500. What is clear is that he isn't killing it. I mean, if the fricking creator of jQuery can't make a fuck ton of cash on a JavaScript book...really, what chance do the rest of us have? It's John Fucking Resign, writing about JavaFuckingScript! Shouldn't this be the #1 best seller of all time, crushing The Bible like an empty Miller High Life against it's forehead?
This book is so full of typos, grammar mistakes and bad code. Where should I send my bitchy emails?
Take your pick - all of these people took the time to proof read it and gave me immensely helpful feedback.
Paddy Foran (blog) - Paddy sent me about 100 pages of speling, grammer and typeos, which is amazing since the book is only 64 pages. I dare you to find a mistake that he missed and then harass him endlessly about it. I DARE YOU.
Shawn Poulson (blog, friendfeed, twitter) - Shawn helped fix my atrocious PERL regular expression skills as well as suggesting using the Heredoc syntax when including JavaScript in views.
Jonathan Langevin (site, cakephp forum) - Jonathan sent me a bunch of spelling errors, which I probably could have figured out on my own if I those damn squiggly red lines under the words weren't distracting me so much.
Heath and Sohaib - both challenged me on the "Getting the Logged In User from Anywhere" section.
Felix Geisendörfer (site) - Felix is a superstar and provided some great pointers.
I'm sure I'm missing someone. If you're that someone, know that as angry as you are for not being mentioned, I feel 1/8 as bad for missing you.
What as the most significant thing you learned while writing this?
I feel like a moron for not knowing this. Apparently the correct slang is friggin'. But for years I've been using "friggen" - which has a completely different meaning. It's no wonder that my friends would act strange when I'd ask "anyone want to hang at my place and play video games and other friggen stuff?" Then they'd show up five minutes later and always seem disappointed at 3AM when I said I was going to bed...like they were expecting more...
This brings up an important issue. Has there ever been cursing in a programming book before?
Not that I've ever seen, but I'd be interested in hearing about it. My goal is to have the first NSFW programming book. Set your dreams high, kids.
I have question/comment about the book. How should I contact you?
Send me an email. Hit me up on twitter. Leave a comment on this post.
What's next?
Not much for the book. I'll probably fix it up and add new sections from time to time. I've already received a few good suggestions. I have a couple good blog posts ready, including "How To Program While Shitfaced Drunk" and "Stop Fucking With Your JavaScript". They just need a few more drunken tangents and they'll be ready to go. Keep an eye out next week.

44 Comments
Kidding, kidding. This is a really comprehensive collection of tutorials, and we'll make sure it gets distributed far and wide.
Great resource Matt!
Well done, thanks for the book!
Some of this tips i've found already by myself, although i've some questions and ideas for improvement.
As on the page 39, i'm using similar merge function to init some settings. My approach is to declare all possible params and merge it with incoming array:
$settings = array('def_param_1'=>false, 'def_param_2'=>false, 'def_param_3'=>false );
$get_params = $this->params['named'];// url/controller/action/def_param_2:123/hacked_param:zzz
$settings = array_merge( $settings, array_intersect_key($get_params,$settings) );
$settings //def_param_1=false, def_param_2=123, def_param_3 = false
I found it very useful if using many named parameters in the view.
As on the page 38, i'm trying to combine add/edit action in one.
I'm developing multi-user app, so i need to check if the entry that user trying to edit really belongs to him, if he saves new entry then save his user_id too:
function admin_edit($id = null){
$user_id = $this->Session->read('User.id');
$data = $this->Model->findByIdAndClientId($id, $user_id);
if(empty($this->data)) {
$this->data = $data;//just show
} else {
$this->data['Model']['user_id'] = $user_id;
if($data) {//update
$this->data['Model']['id'] = $id;
} else {//create
$this->data['Model']['id'] = '';
}
if($this->Model->save($this->data){
//Ok
}
}
}
There is no overhead becouse of findBy-statement if user will to add new entry since findBy will just return false/null if first '$id' param in null.
I might just ditch my static userData property on AppModel :D
GOOD DEAL, I'M IN! I WILL ALSO BEWARE OF THE CAKE. THE CAKE IS EVIL. I SHALL BOW BEFORE ORGASMO SPERMLICKER!
At least it continued the hilarity of the initial topic. :P Thanks for the tips, Matt!
Actually this is why I picked Cake. I check out CI, Zend and Syfony, but realized these frameworks weren't conducive to neglecting my family and personal obligations to produce a book with no financial reward. CakePHP FTW!
Matt, thanks so much for doing and sharing this.
thanks a lot for this nice book.
I hope it will grow.
I noticed a little typo in the second code of page 39; missing a end bracket for Array :
'detail' => true),
$options);
On page 53 on the last line you write "ypu" rather then "you" :P. Great book btw :D!
Nheee, thanks anyways, its a really good "book", its my fault felling stupid.
Please keep the good work :)
Thanks for this great work.
I tried to implement my own find type your way. I found some typos (two underscores instead of one in the name of the function) but it works perfectly.
I just find a case where it doesn't.
I use my own News::find('latest') in my AppController::beforeFilter() callback to fetch the latest news and display them on all pages.
When I want to display a page from a plugin (and not from my application) : it doesn't work. It seems that instead of looking up the _findLatest method in NewsModel, it tries to find it in my [Plugin]AppModel. As it doesn't find it, there's a fatal error. Strange isn't it?
I haven't tried this with plugins yet, but if you plugin AppModel extends the app AppModel, then I would expect it to work fine.
var $belongsTo = array('CreatedUser' => array('className' => 'User',
'foreignKey' => 'created_user_id'),
'ModifiedUser' => array('className' => 'User',
'foreignKey' => 'modified_user_id'));
Since Cake will use those FK by default, is it really necessary to set them?
Thnx Matt!
John Resig received $7,500 as an advance, just thought you guys would be interested, not much. How do I know? It's actually in his post, top right text of the letter "Advance = $7,500.00"
I'm still quite new to CakePHP so I will take this as some sort of source for "what was CakePHP able to do a few months ago?", but it would be very useful to have updated informations about deprecated or improved stuff (not that I learn things that are really outdated already).
But very nice work anyway, thanks (should have found this a few weeks ago already)! :-)