Icon

Following the Footsteps of Heros, Never Lead to the Straight and Grey Roads. (Oh, Sleeper)

Frodosghost

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 modelrepresents 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.1

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.

 PHP |  copy code |? 
01
// Location :: App/Models/post.php
02
class Post extends AppModel {
03
    var $name = 'Post';
04
 
05
    var $hasMany = array('Comment');
06
}
07
 
08
// Location :: App/Models/comment.php
09
class Comment extends AppModel {
10
    var $name = 'Comment';
11
 
12
    var $belongsTo= array('Post');
13
}

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.

 PHP |  copy code |? 
1
class PostsController extends AppController {
2
 var $name = 'Posts';
3
 var $scaffold;
4
 
5
 function index(){
6
 $posts = $this->Post->find('all');
7
 $this->set('posts', $posts);
8
 }
9
}

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)

 PHP |  copy code |? 
1
// Location :: App/Views/posts/index.ctp
2
debug($posts); //This will display the array like a print_r() in php. It is your friend.
3
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.
  1. MVC Wikipedia []

CakePHP and CronTab in Plesk

My latest little project has needed some automation to take it beyond me visiting a link everyday. I wanted to have a script that I had built in CakePHP just run at intervals, so that I wouldn’t have to manually run it. I was pointed in the direction of Cron a “… a time-based scheduling service in Unix-like computer operating systems” (link). Setting it up to run with CakePHP was rather simple, so lets see how. Read the rest of this entry »

Hunz – When Victims Fight

The long awaited album arrived today. It was worth the wait.
I should start by saying that I have been a fan of Hunz since his days in Beanbag. They were one of the first bands I actually followed, but I only did get one chance to see them live, at Sonfest. It was a sad day when they ended while in America – and while it was probably time for those guys it was difficult to understand – especially because Well-Adjusted seemed to be where they were getting warmed up.

Anyway enough of the past. I’m over it now, I swear.

Another aside. This CD doesn’t not fall into my general taste of music, so you’ll have to excuse if my words and descriptions don’t fit.

Read the rest of this entry »