8 min read

The ingredients are fresh, sliced up, and in place. The oven is switched on, heated, and burning red. It is time for us to put on the cooking hat, and start making some delicious cake recipes. So, are you ready, baker?

In this article, we are going to develop a small application that we’ll call the “CakeTooDoo”. It will be a simple to-do-list application, which will keep record of the things that we need to do. A shopping list, chapters to study for an exam, list of people you hate, and list of girls you had a crush on are all examples of lists. CakeTooDoo will allow us to keep an updated list. We will be able to view all the tasks, add new tasks, and tick the tasks that are done and much more. Here’s another example of a to-do list, things that we are going to cover in this article:

  • Make sure Cake is properly installed for CakeTooDoo
  • Understand the features of CakeTooDoo
  • Create and configure the CakeTooDoo database
  • Write our first Cake model
  • Write our first Cake controller
  • Build a list that shows all the tasks in CakeTooDoo
  • Create a form to add new tasks to CakeTooDoo
  • Create another form to edit tasks in the to-do list
  • Have a data validation rule to make sure users do not enter empty task title
  • Add functionality to delete a task from the list
  • Make separate lists for completed and pending Tasks
  • Make the creation and modification time of a task look nicer
  • Create a homepage for CakeTooDoo

Making Sure the Oven is Ready

Before we start with CakeTooDoo, let’s make sure that our oven is ready. But just to make sure that we do not run into any problem later, here is a check list of things that should already be in place:

  1. Apache is properly installed and running in the local machine.
  2. MySQL database server is installed and running in the local machine.
  3. PHP, version 4.3.2 or higher, is installed and working with Apache.
  4. The latest 1.2 version of CakePHP is being used.
  5. Apache mod_rewrite module is switched on.
  6. AllowOverride is set to all for the web root directory in the Apache configuration file httpd.conf.
  7. CakePHP is extracted and placed in the web root directory of Apache.
  8. Apache has write access for the tmp directory of CakePHP.

In this case, we are going to rename the Cake directory to it CakeTooDoo.

CakeTooDoo: a Simple To-do List Application

As we already know, CakeTooDoo will be a simple to-do list. The list will consist of many tasks that we want to do. Each task will consist of a title and a status. The title will indicate the thing that we need to do, and the status will keep record of whether the task has been completed or not. Along with the title and the status, each task will also record the time when the task has been created and last modified.

Using CakeTooDoo, we will be able to add new tasks, change the status of a task, delete a task, and view all the tasks. Specifically, CakeTooDoo will allow us to do the following things:

  1. View all tasks in the list
  2. Add a new task to the list
  3. Edit a task to change its status
  4. View all completed tasks
  5. View all pen
  6. Delete a task
  7. A homepage that will allow access to all the features.

You may think that there is a huge gap between knowing what to make and actually making it. But wait! With Cake, that’s not true at all! We are just 10 minutes away from the fully functional and working CakeTooDoo. Don’t believe me? Just keep reading and you will find it out yourself.

Configuring Cake to Work with a Database

The first thing we need to do is to create the database that our application will use. Creating database for Cake applications are no different than any other database that you may have created before. But, we just need to follow a few simple naming rules or conventions while creating tables for our database. Once the database is in place, the next step is to tell Cake to use the database.

Time for Action: Creating and Configuring the Database

  1. Create a database named caketoodoo in the local machine’s MySQL server. In your favourite MySQL client, execute the following code:
    CREATE DATABASE caketoodoo;
  2. In our newly created database, create a table named tasks, by running the following code in your MySQL client:
    USE caketoodoo; 
    CREATE TABLE tasks (
    id int(10) unsigned NOT NULL auto_increment,
    title varchar(255) NOT NULL,
    done tinyint(1) default NULL,
    created datetime default NULL,
    modified datetime default NULL,
    PRIMARY KEY (id)
    );

  3. Rename the main cake directory to CakeTooDoo, if you haven’t done that yet.
  4. Move inside the directory CakeTooDoo/app/config. In the config directory, there is a file named database.php.default. Rename this file to database.php.
  5. Open the database.php file with your favourite editor, and move to line number 73, where we will find an array named $default. This array contains database connection options. Assign login to the database user you will be using and password to the password of that user. Assign database to caketoodoo. If we are using the database user ahsan with password sims, the configuration will look like this:
    var $default = array( 
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'port' => '',
    'login' => 'ahsan',
    'password' => 'sims',
    'database' => 'caketoodoo',
    'schema' => '',
    'prefix' => '',
    'encoding' => ''
    );
  6. Now, let us check if Cake is being able to connect to the database. Fire up a browser, and point to http://localhost/CakeTooDoo/. We should get the default Cake page that will have the following two lines: Your database configuration file is present and Cake is able to connect to the database, as shown in the following screen shot. If you get the lines, we have successfully configured Cake to use the caketoodoo database.

    Create a Quick Application in CakePHP: Part 1

What Just Happened?

We just created our first database, following Cake convention, and configured Cake to use that database.

Our database, which we named caketoodoo, has only one table named task. It is a convention in Cake to have plural words for table names. Tasks, users, posts, and comments are all valid names for database tables in Cake. Our table tasks has a primary key named id. All tables in Cake applications’ database must have id as the primary key for the table.

Conventions in CakePHP
Database tables used with CakePHP should have plural names. All database tables should have a field named id as the primary key of the table.

We then configured Cake to use the caketoodoo database. This was achieved by having a file named database.php in the configuration directory of the application. In database.php, we set the default database to caketoodoo. We also set the database username and password that Cake will use to connect to the database server.

Lastly, we made sure that Cake was able to connect to our database, by checking the default Cake page.

Conventions in Cake are what make the magic happen. By favoring convention over configuration, Cake makes productivity increase to a scary level without any loss to flexibility. We do not need to spend hours setting configuration values to just make the application run. Setting the database name is the only configuration that we will need, everything else will be figured out “automagically” by Cake. Throughout this article, we will get to know more conventions that Cake follows.

 

Writing our First Model

Now that Cake is configured to work with the caketoodoo database, it’s time to write our first model. In Cake, each database table should have a corresponding model. The model will be responsible for accessing and modifying data in the table. As we know, our database has only one table named tasks. So, we will need to define only one model. Here is how we will be doing it:

Time for Action: Creating the Task Model

  1. Move into the directory CakeTooDoo/app/models. Here, create a file named task.php.
  2. In the file task.php, write the following code:
    <?php 
    class Task extends AppModel {
    var $name = 'Task';
    }
    ?>
  3. Make sure there are no white spaces or tabs before the <?php tag and after the ?> tag. Then save the file.

What Just Happened?

We just created our first Cake model for the database table tasks. All the models in a CakePHP application are placed in the directory named models in the app directory.

Conventions in CakePHP: All model files are kept in the directory named models under the app directory.

Normally, each database table will have a corresponding file (model) in this directory. The file name for a model has to be singular of the corresponding database table name followed by the .php extension. The model file for the tasks database table is therefore named task.php.

Conventions in CakePHP: The model filename should be singular of the corresponding database table name.

Models basically contain a PHP class. The name of the class is also singular of the database table name, but this time it is CamelCased. The name of our model is therefore Task.

Conventions in CakePHP: A model class name is also singular of the name of the database table that it represents.

You will notice that this class inherits another class named AppModel. All models in CakePHP must inherit this class.

The AppModel class inherits another class called Model. Model is a core CakePHP class that has all the basic functions to add, modify, delete, and access data from the database. By inheriting this class, all the models will also be able to call these functions, thus we do not need to define them separately each time we have a new model. All we need to do is to inherit the AppModel class for all our models.

We then defined a variable named $name in the Task’model, and assigned the name of the model to it. This is not mandatory, as Cake can figure out the name of the model automatically. But, it is a good practice to name it manually.

LEAVE A REPLY

Please enter your comment!
Please enter your name here