CakePHP and the MVC
Getting used to the Models, Views and Controllers can be a headache when first stepping into a framework that operates like this. It is one of the first hurdles to get over, that is why you see people commenting that ‘once they understood Cakephp it was simple’ to work with.
So its greatest and most simplelist aspect is one that has to be learned. So what about taking that step?
The MVC is best explained by wikipedia :
In MVC, the model represents the information (the data) of the application; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages the communication of data and the business rules used to manipulate the data to and from the model. ((MVC Wikipedia))
So it seems to make sence there. But what about in action. I shall use a typical blog situation to explain how to relate the three, and you can interpret from there.
I should say right here that CakePHP has a book, which is one of the most useful little items to have in your toolkit, so read up there first, because it explains a lot of how to do all this cake gear. Like the MVC overview is here. As the bible is to life, so The Book is to CakePHP.
Model: This is the part that holds all our logic for data relationships. If our Posts have many Comments, then this is where we let Cake know that relationship – not forgetting we should also say our Comments belongTo a Post. It is that simple to start off with, just let the two relate like they should when you say it out loud.
// Location :: App/Models/post.php
class Post extends AppModel {
var $name = 'Post';
var $hasMany = array('Comment');
}
// Location :: App/Models/comment.php
class Comment extends AppModel {
var $name = 'Comment';
var $belongsTo= array('Post');
}
Controller: Here we prepare a our data to go to the view. So we simply just call a Post from our model, tell it to find all with a query, and send it to the view. Since the relationship has been set in the Model, we don’t need to worry about the comments, each Post will have the Comment in the array it returns.
The $this->set sets the $posts in the view.
class PostsController extends AppController {
var $name = 'Posts';
var $scaffold;
function index(){
$posts = $this->Post->find('all');
$this->set('posts', $posts);
}
}
View: To cheat here, just simply put a debug call in the view. This will show the array as returned from the controller. You can use a foreach loop to repeat the data into table rows or such like… (updated code below, with thanks to evilbloodydemon)
// Location :: App/Views/posts/index.ctp
debug($posts); //This will display the array like a print_r() in php. It is your friend.
foreach($posts as $post):
echo $post['Post']['title'];
foreach($post['Comment'] as $comment):
echo $comment['content'];
endforeach;
endforeach;
So the run through theory is simple. I have this little app running at domain.com.
I go to domain.com/posts – which the controller realises is the index of posts. Finds that method in the PostsController, does the find and sends it to the view. Easy. Always remember that you should set up the relationships first, and the rest should all fall into place.
evilbloodydemon@gmail.com
last example should be:
foreach($posts as $post):
echo $post[‘Post’][‘title’];
foreach($post[‘Comment’] as $comment):
echo $comment[‘content’];
endforeach;
endforeach;
because of Post hasMany Comment relationship.
jamesmonkey@gmail.com
Thanks for pointing that out. Code changed above.