Binding your Models the Easy Way
Finally, I’ve posted my first cakephp blog which is already a week late. So late that I really had a hard time picking a nice topic to make it worth the wait. I don’t know if this one is but I hope it’ll be helpful especially to those who are new to cake. Ive been working on IsupportThisMessage.com for almost a year and still supporting it until now since they’re planning to add more functionalities to the system. This is where I learned the basics of cake.The manual is quite simple and I’ve read it over and over. But when I thought I already know a lot and started coding, I realized, I don’t even know a bit. So really, there’s no better way of learning than actually applying it. And for almost a year, I’ve learned a lot but I must admit, I still have a lot to learn.
One nice thing I like about cake is the use of MVC pattern which makes my codes more organized. And I like how the querying is done which is made more simple because you wouldn’t need to declare associated tables within your query if you have already specified the associations within your model. Fantastic! One of the most powerful features of CakePHP is the relational mapping provided by the model. At first, I didn’t want to touch anything in the models coz I was confused about associations. Besides I was scared I might break the code. But when I finally understood how, I declared the necessary associations within my models most especially “messages”, coz in isupport this is often being used and it has a lot of associated tables in it. But sometimes, when you query, your association settings in the model file are giving you too much (or not enough) information. What I mean to say is, sometimes you wouldn’t need some of the associated tables in your query result. So you have to think of a way how to minimize the result, and that is where unbindModel & bindModel model functions are used which allow you to change your association settings on the fly. But as quoted from the bakery section of cakephp.org by TommyO,
Changing the requirements of a bind done in this way means going through your controllers and changing the bindModel call, often in multiple places. Using unbindModel in a controller also means every time a new association is added you may need to go back into your controllers and unbind the new associations in order to optimize your code.
Silly, but true. Thanks to TommyO, he created a method to simplify this using the approach:
binding to other Models only when needed or unbinding existing relations to minimize the size of your result set.
But just an overview, you would just need to add a function within your appmodel. Then in your model class, just define your associations within an array called $assoc. You will see an example from the link. So easy huh?
So in your controller:
<?php
MessageController extends AppController {
function index($id) {
// establish necessary associations
$this->Message->expects(array(‘Comments’, ‘User’));
$results = $this->Message->findAll();
}
}
?>
Now you have it. But that’s not all. Ive read another post in the bakery related to TommyO’s method for model associations. In TommyO’s approach, there’s a slight difference on the way you define your model association and the models get loaded *when* you call the expects() function. According to Mariano, the author of the post, he said he’s not a fan of changing how things are done in cakephp, neither do I. His approach is to use the Cakephp way of defining associations in the models and then specify which relations he’s interested in getting back when querying a model. This way, models behave the way models are supposed to. The main idea here is that you define your associations in the model the cakephp way. Then in your controller, if you want the standard result cakephp brings then you don’t need to call expects() (With TommyO’s approach, you will need to call expects() with all the necessary values). Use expects(), only when you want to limit the result of your query. And if you want to unbindAll just call expects() with no parameters. You will need to add three functions in your appmodel which you will find here. There are additional topics included here like “making multiple expects in one call“. And if I’m a little confusing in explaining, I think you can understand it better when you visit the links. hehehehe.
Good luck!
Tags: bindModel, unbindModel

February 6th, 2008 at 4:03 am
Hey, have you noticed the first few sentences have rhymes?
Finally, I’ve posted my first cakephp blog which is already a week *late*.
So late that I really had a hard time picking a nice topic to make it worth the *wait*.
I don’t know if this one is but I hope it’ll be helpful especially to those who are new to *cake*.
nice one ^__^