Scaffolding in CakePHP

|
| By Webner

Scaffolding in CakePHP

Application scaffolding in CakePHP is a technique that allows the developer to define and create a basic application that can create, retrieve, update and delete objects. Basically CRUD (create, Read, Update, Delete) data operations are common in most of the applications.

Scaffolding basically provides temporary structure or loose structure to build up an application quickly. Its limitation is that it is not flexible to customize logic and views. Let’s see how an application can be developed with Scaffolding in CakePHP.

Let’s take an example of products application.

Step 1: Create Database
For Products based application, let’s have following tables in the database:
categories (id, name);
products (id, name, price, brand, category_id);
category_id in products table is foreign key belongs to primary key in categories table

Let’s create table in mysql using following queries:

CREATE TABLE categories(
    id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
);

CREATE TABLE products(
    id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    price int,
    brand varchar(100),
    category_id int references categories(id)
);

Step 2: Create Your controller files
Now, we will create CategoriesController.php and ProductsController.php in CakePHP/app/Controller. (Note:- Remember to follow and understand file naming conventions). Now, we will write following code in Controller files:

In CategoriesController.php file,

 class CategoriesController extends AppController{
var $scaffold;
}

In ProductsController.php file,

 class ProductsController extends AppController{
var $scaffold;
}

In both controller files, we have added $scaffold variable to specify that this application is based on Scaffolding approach. No other code is required in case of Scaffolding approach in CakePHP Application. Now, we will create Model files, named: Category.php and Product.php
In Category.php file, following code is written,

 class Category extends AppModel{
var $name = 'Category';
var $displayField = 'name';
}

In Product.php file, following code is written,

 class Product extends AppModel{
var $name = Product;
var $displayField = 'name}

After this little bit coding, now we are ready to run our application.

Step 3: Run your sample application
Now open your domain in the browser, whether in localhost or in hosted server like below:

http://localhost/cakephp/Categories (localhost)
http://somedomain.com/Categories (on hosted server)

After domain name, write name of the controller (Categories) as in above example URLs.
The following screenshot will be displayed:

1
Fig 1: categories view page

Right now, no category is displaying in Categories List, because there is no category in the database. Now click on New Category button at left-hand side of Actions Panel. Following screen will be displayed to add a new category.

2
Fig 2: add new category Screen

After adding one or more categories, following screen will be displayed:
3
Fig 3: List categories

In above screenshot, we can see that there is View/Edit/Delete actions on the right side where List of Categories is displaying. Similarly, we can add new products in products table by clicking on New Product Button showing at Actions Panel at Left Hand Side. After Adding two or more Products, we can have the following screenshot for Products:

4
Fig 4: List Products

In this way, we can perform CRUD operations by using Scaffolding technique in CakePHP MVC framework and application can go for production. In this way, we have seen that how it is pretty cool to develop an application within few minutes.

Leave a Reply

Your email address will not be published. Required fields are marked *