View, Insert, Update and Delete Operations in CakePHP

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, Delete (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:\xampp\htdocs\cakephp_example\app\Model

    Open the demomovie.php file.

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

    Controller

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

    C:\xampp\htdocs\cakephp_example\app\Controller


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

    1. <?php  
    2. class DemomoviesController extends AppController {  
    3.   
    4.    public $components = array('Session');  
    5.   
    6.    public function index()  
    7.   
    8.    {  
    9.   
    10.       $movies = $this->Demomovie->find('all');  
    11.   
    12.       $this->set('demomovies'$movies);  
    13.   
    14.    }  
    15.   
    16.    public function add()  
    17.   
    18.    {  
    19.   
    20.       if (!emptyempty($this->data)) {  
    21.   
    22.          $this->Demomovie->create($this->data);  
    23.   
    24.          if ($this->Demomovie->save()) {  
    25.   
    26.             $this->Session->setFlash('The movie has been saved');  
    27.   
    28.             $this->redirect(array('action' => 'add'));  
    29.   
    30.          } else {  
    31.   
    32.             $this->Session->setFlash  
    33.   
    34.             ('The movie could not be saved. Please, try again.');  
    35.   
    36.          }  
    37.   
    38.       }  
    39.   
    40.    }  
    41.   
    42.    public function delete($id = null)  
    43.   
    44.    {  
    45.   
    46.       if (!$id) {  
    47.   
    48.          $this->Session->setFlash('Invalid id for movie');  
    49.   
    50.          $this->redirect(array('action' => 'index'));  
    51.   
    52.       }  
    53.   
    54.       if ($this->Demomovie->delete($id)) {  
    55.   
    56.       $this->Session->setFlash('Movie deleted');  
    57.   
    58.       } else {  
    59.   
    60.          $this->Session->setFlash(__('Movie was not deleted',  
    61.   
    62.          true));  
    63.   
    64.       }  
    65.   
    66.       $this->redirect(array('action' => 'index'));  
    67.   
    68.    }  
    69.   
    70.    function edit($id = null) {  
    71.   
    72.       if (!$id && emptyempty($this->data)) {  
    73.   
    74.          $this->Session->setFlash('Invalid movie');  
    75.   
    76.          $this->redirect(array('action' => 'index'));  
    77.   
    78.       }  
    79.   
    80.       if (!emptyempty($this->data)) {  
    81.   
    82.          if ($this->Demomovie->save($this->data)) {  
    83.   
    84.             $this->Session->setFlash('The movie has been saved');  
    85.   
    86.             $this->redirect(array('action' => 'index'));  
    87.   
    88.          } else {  
    89.   
    90.          $this->Session->setFlash('The movie could not be saved. Please, try again.');  
    91.   
    92.       }  
    93.   
    94.    }  
    95.   
    96.    if (emptyempty($this->data)) {  
    97.   
    98.       $this->data = $this->Demomovie->read(null, $id);  
    99.   
    100.    }  
    101.   
    102. }  
    103.   
    104. function view($id = null) {  
    105.   
    106.    if (!$id) {  
    107.   
    108.       $this->Session->setFlash('Invalid movie');  
    109.   
    110.       $this->redirect(array('action' => 'index'));  
    111.   
    112.    }  
    113.   
    114.    $this->set('movie'$this->Demomovie->findById($id));  
    115.   
    116.    }  
    117.   
    118. }  
    119.   
    120. ?>  
     View

    Create a Demomovies folder.

     

    C:\xampp\htdocs\cakephp_example\app\View 

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

    C:\xampp\htdocs\cakephp_example\app\View\Demomovies

    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:

     

    Up Next
      Ebook Download
      View all
      Learn
      View all