on Tutorials

CakePHP from scratch: Baking with dependancy tables

2 comments
CakePHP baking a controller
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

In the last tutorial I showed how easy it is to bake model, controller and view for our Categories table. I am repeating for those who are new, we are building a Jobs board application which will be used to post a job opening in a certain category, to list, filter and search the jobs, to register and login, to apply to a job and other interesting stuff.

Along the way, I hope that I will show you all aspects of this incredible framework and convince you to start using it.

So, if you created the tables in our database, please drop them, as I added some new fields. Here are the new tables:

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `jobs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `title` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
  `body` text COLLATE utf8_unicode_ci,
  `company` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `job_type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `pass` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `web` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

We will bake files for all 3 tables to establish our relationships. As you can see if you look at the Jobs table structure, it has category_id and user_id fields. This two fields will be used to create relationships (associations) between the tables.

There is a naming convention which must be followed if we want Bake to generate a proper relationship. Field must be in singular form of the relationship’s table name (table is called Categories, so we have category_id field).

So, let’s get Baking. Go to your command prompt browse to your working directory and type in cake bake. Type M for baking a model and press Enter. Choose your default database configuration by pressing Enter.

Type in 1 for baking our Categories model. Script asks you to provide a validation criteria, so type in y for yes. Choose 28 for id, 19 (not empty) for name field.

Now, the interesting part. Bake script is asking if we want to build Model association. Type in y and press Enter. Bake will correctly ask if Category hasMany Job. So answer yes.

After that answer no and again yes if everything looks OK. If you followed previous tutorials, it will ask you to overwrite the file category.php, please do so.

We skip the SimpleTest question by always answering No.

OK, let’s bake our Job model, so M for model, 2 for Job. For validation, choose yes and these rules: for id field type in 28, for category_id and users_id type in 19 (not empty), you can put 19 (not empty) for title, body, company and job_type fields.

Now, when we come to our association criteria, Bake asks as if Job belongs to Category, the answer is Yes. And after correctly asking if Job belongs to User, answer is again Yes.

Now, do the same for the users table.You can see on the image below how I managed my validation and association criteria:

It is now time to bake our controllers. So type in C and press Enter and Enter again for default database configuration. Type in 1 for Categories controller and yes to build it interactively. Then choose no for scaffolding, yes for creating some basic methods and no for admin routing. Type no for both Helpers and Components question and yes for Session messages. On the image below is the whole process:

Repeat this steps for other two controllers. Our next step is to build our views for all three controllers. So type in V and follow the steps on picture below:

Repeat this steps for remaining two controllers. We are finished. Point your browser to your Cake application and Categories controller (for example: http://localhost/cake/categories) and try to add some users and categories.

When you go to Add new job, you will see options to choose category and user.


Now we have a full working application. Obviously the work is not finished, so stay tuned. Next time, we are going to adjust some things that Cake Baked, like error messages and similar.

Make sure your read our previous tutorials in CakePHP series:

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon