View, Insert, Update and Delete Operations in CakePHP – Info PHP

CakePHP is an open source PHP framework built around Model-View-Controller (MVC). We will begin to create a small application that will do some basic Create, Read, Update, (CRUD) operations on our database.

  • Model: It manages the data. It stores into and retrieves from the database.
  • View: It is for the presentation and is responsible for displaying the data provided by the model in a specific format.
  • Controller: Handles the model and view layers to work together. 
View, add, edit and delete functionality. You can download the full tutorial here.

 

 

Step1: The Database Structure

To create a database and table:

  1. CREATE TABLE IF NOT EXISTS `demomovies` (    
  2.     
  3.    `id` char(36) NOT NULL,    
  4.     
  5.    `title` varchar(255) DEFAULT NULL,    
  6.     
  7.    `genre` varchar(45) DEFAULT NULL,    
  8.       
  9.    `rating` varchar(45) DEFAULT NULL,    
  10.     
  11.    `format` varchar(45) DEFAULT NULL,    
  12.     
  13.    `length` varchar(45) DEFAULT NULL,    
  14.     
  15.    `created` datetime DEFAULT NULL,    
  16.     
  17.    `modified` datetime DEFAULT NULL,    
  18.     
  19.    PRIMARY KEY (`id`)    
  20.     
  21. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;  
  1. INSERT INTO `demomovies` (`id`, `title`, `genre`, `rating`, `format`, `length`, `created`, `modified`) VALUES    
  2.     
  3. (’54d9874c-ae74-4451-b54a-14f01bafaffa’‘secondfirst’‘sdfsd’‘4.5’‘sdf’‘sdf’‘2015-02-10 05:21:32’‘2015-02-10 05:34:01’),    
  4.     
  5. (’54d98aa7-d65c-40cd-8b7f-14f01bafaffa’‘second’‘firest’‘5.4’‘dsf’‘sadf’‘2015-02-10 05:35:51’‘2015-02-10 05:35:51’);    

Step 2: Create Model, Controller and View

Create a demomovie.php file and save it in the following path.

C:xampphtdocscakephp_exampleappModel

Open the demomovie.php file.

 
  1. class Demomovie extends AppModel {  
  2.   
  3.    var $name = ‘demomovie’;  
  4.   
  5.    var $validate =array(  
  6.     
  7.       ‘title’ =>array(  
  8.   
  9.          ‘alphaNumeric’ =>array(  
  10.   
  11.             ‘rule’ => array(‘minLength’,2),  
  12.   
  13.             ‘required’ => true,  
  14.   
  15.             ‘message’ => ‘Enter should be minimum 2 only’)  
  16.   
  17.          ),  
  18.   
  19.          ‘genre’ =>array(  
  20.   
  21.             ‘alphaNumeric’ =>array(  
  22.   
  23.                ‘rule’ => array(‘minLength’,4),  
  24.   
  25.                ‘required’ => true,  
  26.   
  27.                ‘message’ => ‘Enter should be minimum 4 only’)  
  28.   
  29.          ),  
  30.   
  31.          ‘rating’ =>array(  
  32.   
  33.             ‘alphaNumeric’ =>array(  
  34.   
  35.             ‘rule’ => array(‘minLength’,2),  
  36.   
  37.             ‘required’ => true,  
  38.   
  39.             ‘message’ => ‘Enter should be minimum 4 only’)  
  40.   
  41.          )  
  42.   
  43.       );  
  44.   
  45.    }  
  46.   
  47. ?>    
  48.    

Save it.

Controller

Create DemomoviesController.php and save it in the following path.

C:xampphtdocscakephp_exampleappController

Open DemomoviesController.php files and paste in the following code.

 View

Create a Demomovies folder.

 

C:xampphtdocscakephp_exampleappView 

Create add.ctp , edit.ctp , index.ctp , view.ctp files

C:xampphtdocscakephp_exampleappViewDemomovies

Set route

Open routes.php with the following pat.

  1. Router::connect(‘/’array(‘controller’ => ‘demomovies’‘action’ => ‘index’‘home’));  

Save it.

The following are screen shots of view, add, edit, and delete.

View

Add

Edit

After edit form:

 

Article Prepared by Ollala Corp

You might also like More from author

Leave A Reply

Your email address will not be published.