CakePHP tutorial Day-1 Part-2

Posted: July 19, 2014 in Uncategorized

<<Go to CakePHP tutorial Day-1 Part-1

Day-1  (Part-2) : 

Before starting the project, you should understand about Model, View and Controller in a brief. You can refer book.cakephp.org further for more details.

Model:

Models are the classes that form the business layer in your application. They should be responsible for managing almost everything regarding your data, its validity, and its interactions, as well as the evolution of the information workflow in your domain. We are going to put all our models at “/app/Model” folder.

Controller:

After routing has been applied and the correct controller has been found, your controller’s action is called. You will understand this when we start the project. We are going to put all our models at “/app/Controller” folder.

View:

The view layer of CakePHP is how you speak to your users.  It represents the HTML output on the screen. It refers what and how you are going to represent the output on the screen. We are going to put all our models at “/app/View” folder.
Project Brief:

I am going to develop one simple DVD management application with CakePHP.  For my DVD Catalog I want an application that will manage my ever growing collection of DVDs ( films and TV shows ). For each DVD that is entered into the application I want to add an image ( preferably a cover image ), a website URL, the Imdb website entry and a personal rating.

Along with being able to add / edit / delete DVDs I would like a front end display so that other people can go online and browse my collection as well. This front end will have search and filter functionality so that I can search by name or filter by format type etc. The front end also needs to be fully customisable so I’m going to implement a templating system where I can easily change the layout in the admin panel.

Note: Similar project is done in CakePHP1.2 by James   which is not working now in CakePHP 2.5.2.  I am going to make it more simple and develop with CakePHP 2.5.2

1. You have created database “tut”. Now let’s create tables. Before that, here is the understanding.

Good planning is paramount to any system so here are the requirements for the DVD Catalog Application:

  • Formats
    • These are the different formats of DVD that will exist in the system.
    • A few examples of format types include dvd, dvdcopy, dvdrip, tvrip and hd.
    • Be able to add / edit / delete format types.
    • Be able to sort and filter DVDs by format type.
  • Types
    • These are the different types of DVD in the system.
    • Examples include Films and TV Shows.
    • Be able to add / edit / delete types.
    • Be able to sort and filter DVDs by Type.
  • Locations
    • These are the different locations where your DVDs will be stored.
    • Examples include spindle 1, spindle 2, etc.
    • Be able to add / edit / delete locations.
    • Be able to sort and filter DVDs by Location.
  • DVDs
    • These are the actual DVDs that you will enter into the system.
    • Each DVD will have a number of attributes:
      • The Format Type, is it a DVD? a Copied DVD? A DVD Rip?.
      • The Type, is it a Film? Is it a TV Show?
      • The Location.
      • Name.
      • Image URL.
      • Official Website.
      • Imdb Link.
      • Rating.
      • Number of Discs.
      • Number of Episodes if its a TV Show.
  • Genres
    • A DVD will be able to belong to a number of genres.
    • A Genre will have multiple DVDs.
    • An Admin will be able to add /edit / delete Genres from the system
    • A user will be able to:
      • Filter by Genre.
      • Search by Genre.
  • Admin Section
    • The application will have a password protected admin area where a logged in admin will be able to interact with the system.
    • Admins can add / edit / delete:
      • Formats
      • Types
      • Locations
      • DVDs
      • Templates
  • Front End
    • The application will have a front end that will be viewable to anyone.
    • The main front end will show all the DVDs in the system.
    • The number of displayed DVDs will be limited and so pagination will be required.
    • A user will be able to filter the DVDs by:
      • Format
      • Type
      • Location
      • DVD Rating
    • A user will also be able to search for DVDs.
    • A user will be able to bookmark search and filters.
    • A user will be able to select different templates, changing the appearance of the system.
  • Templates
    • The system will be able to incorporate numerous templates.
    • A template will be added by the admin of the application
    • The template name will be used to identify images and css used by the template
    • A user will be able to select the template to change the design.

      Database Structure

      From the list of requirements I’ve come up with 8 different tables for the database which include Formats, Types, Locations, DVDS, Genres, Templates and Admins.

      • Formats
        • id, name, slug, description, created, modified, status
      • Types
        • id, name, slug, description, created, modified, status
      • Locations
        • id, name, slug, description, created, modified, status
      • DVDs
        • id, format_id, type_id, location_id, name, slug, image, website, imdb, rating, discs, episodes, created, modified, status
      • Genres
        • id, name, slug, created, modified, status
      • DVDS_Genres
        • id, dvd_id, genre_id
      • Templates
        • id, name, layout_name, description, image, created, modified, status
      • Admins
        • id, username, password, last_login, statusNow, let’s start creating tables under database “tut”


Using CakePHP conventions our database table names are all plural and our join table is the plural names of the two tables in alphabetical format. This ensures that CakePHP will automatically be able to connect to the database and create associated data with Cake’s inbuilt Automagic methods

Formats, Types and Locations have exactly the same database fields but I’ve not combined them into one table and used a flag to identify them because I wanted to seperate out the logic for each type and it also makes it easier to sort and filter by the different tables when our system is up and running. The model associations between these tables and the DVD table will also be easier to maintain and establish. A “slug” has been entered that will be used for user friendly URLs so instead of seeing “/formats/view/1” you will see “/formats/view/dvdrip”.

CREATE TABLE `formats` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`slug` VARCHAR( 255 ) NOT NULL ,
`desc` TEXT NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL ,
`status` TINYINT( 1 ) NOT NULL DEFAULT '1',
PRIMARY KEY ( `id` )
);
CREATE TABLE `types` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`slug` VARCHAR( 255 ) NOT NULL ,
`desc` TEXT NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL ,
`status` TINYINT( 1 ) NOT NULL DEFAULT '1',
PRIMARY KEY ( `id` )
);
CREATE TABLE `locations` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`slug` VARCHAR( 255 ) NOT NULL ,
`desc` TEXT NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL ,
`status` TINYINT( 1 ) NOT NULL DEFAULT '1',
PRIMARY KEY ( `id` )
);

 The DVD table will store all our information for each DVD and will also contain “foreign keys” that will link a DVD to a Format, Type and Location.

CREATE TABLE `dvds` (
`id` INT NOT NULL AUTO_INCREMENT ,
`format_id` INT NOT NULL ,
`type_id` INT NOT NULL ,
`location_id` INT NOT NULL ,
`name` VARCHAR( 100 ) NOT NULL ,
`slug` VARCHAR( 100 ) NOT NULL ,
`image` VARCHAR( 255 ) NOT NULL ,
`website` VARCHAR( 255 ) NOT NULL ,
`imdb` VARCHAR( 255 ) NOT NULL ,
`rating` TINYINT NOT NULL ,
`discs` TINYINT NOT NULL ,
`episodes` TINYINT NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL ,
`status` TINYINT( 1 ) NOT NULL DEFAULT '1',
PRIMARY KEY ( `id` )
);

The Genre table will contain the name of the Genre along with a slug for friendly URLs. A Join table will also be needed to link up Genres with DVDs, this will be displayed in much more detail in our model associations but this allows DVDs to have multiple Genres and to allow a Genre to have multiple DVDs.
CREATE TABLE `genres` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`slug` VARCHAR( 255 ) NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL ,
`status` TINYINT( 1 ) NOT NULL DEFAULT '1',
PRIMARY KEY ( `id` )
);

CREATE TABLE `dvds_genres` (
`id` INT NOT NULL AUTO_INCREMENT ,
`dvd_id` INT NOT NULL ,
`genre_id` INT NOT NULL ,
PRIMARY KEY ( `id` )
);

 The templates table will store all the information relatating to the template, the layout name will be used to choose the images that the template will use along with the css file that will change the look and appearence of the front end system.

CREATE TABLE `templates` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `layout_name` varchar(100) NOT NULL,
  `description` text NOT NULL,
  `image` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `status` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
);

The final table (admins) will be used to store the username and password of any user that has access to the admin area of the application.
CREATE TABLE `admins` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `last_login` datetime NOT NULL,
  `status` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
);

So, Now, you have created all tables. Right ?  Take a break, may be by having a coffee.

<<Go to CakePHP tutorial Day-1 Part-1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s