Create Custom Side Menus
Frodo's Ghost | CakePHP and the MVC
post-template-default,single,single-post,postid-38,single-format-standard,eltd-core-1.0.1,ajax_fade,page_not_loaded,,borderland - frodosghost-child-ver-1.0.0,borderland-ver-1.2, vertical_menu_with_scroll,smooth_scroll,side_menu_slide_with_content,width_470,paspartu_enabled,paspartu_on_bottom_fixed,wpb-js-composer js-comp-ver-4.4.4,vc_responsive

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'];

So the run through theory is simple. I have this little app running at

I go to – 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.

  • last example should be:

    foreach($posts as $post):
    echo $post[‘Post’][‘title’];
    foreach($post[‘Comment’] as $comment):
    echo $comment[‘content’];

    because of Post hasMany Comment relationship.

    June 24, 2009 at 6:08 am
  • Thanks for pointing that out. Code changed above.

    June 24, 2009 at 12:25 pm

Post a Comment