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.

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. `id` char(36) NOT NULL,
  3. `title` varchar(255) DEFAULT NULL,
  4. `genre` varchar(45) DEFAULT NULL,

  5. `rating` varchar(45) DEFAULT NULL,
  6. `format` varchar(45) DEFAULT NULL,
  7. `length` varchar(45) DEFAULT NULL,
  8. `created` datetime DEFAULT NULL,
  9. `modified` datetime DEFAULT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  1. INSERT INTO `demomovies` (`id`, `title`, `genre`, `rating`, `format`, `length`, `created`, `modified`) VALUES
  2. ('54d9874c-ae74-4451-b54a-14f01bafaffa', 'secondfirst', 'sdfsd', '4.5', 'sdf', 'sdf', '2015-02-10 05:21:32', '2015-02-10 05:34:01'),
  3. ('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. var $name = 'demomovie';
    4. var $validate =array(

    5. 'title' =>array(
    6. 'alphaNumeric' =>array(
    7. 'rule' => array('minLength',2),
    8. 'required' => true,
    9. 'message' => 'Enter should be minimum 2 only')
    10. ),
    11. 'genre' =>array(
    12. 'alphaNumeric' =>array(
    13. 'rule' => array('minLength',4),
    14. 'required' => true,
    15. 'message' => 'Enter should be minimum 4 only')
    16. ),
    17. 'rating' =>array(
    18. 'alphaNumeric' =>array(
    19. 'rule' => array('minLength',2),
    20. 'required' => true,
    21. 'message' => 'Enter should be minimum 4 only')
    22. )
    23. );
    24. }
    25. ?>
    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. public $components = array('Session');
    4. public function index()
    5. {
    6. $movies = $this->Demomovie->find('all');
    7. $this->set('demomovies', $movies);
    8. }
    9. public function add()
    10. {
    11. if (!emptyempty($this->data)) {
    12. $this->Demomovie->create($this->data);
    13. if ($this->Demomovie->save()) {
    14. $this->Session->setFlash('The movie has been saved');
    15. $this->redirect(array('action' => 'add'));
    16. } else {
    17. $this->Session->setFlash
    18. ('The movie could not be saved. Please, try again.');
    19. }
    20. }
    21. }
    22. public function delete($id = null)
    23. {
    24. if (!$id) {
    25. $this->Session->setFlash('Invalid id for movie');
    26. $this->redirect(array('action' => 'index'));
    27. }
    28. if ($this->Demomovie->delete($id)) {
    29. $this->Session->setFlash('Movie deleted');
    30. } else {
    31. $this->Session->setFlash(__('Movie was not deleted',
    32. true));
    33. }
    34. $this->redirect(array('action' => 'index'));
    35. }
    36. function edit($id = null) {
    37. if (!$id && emptyempty($this->data)) {
    38. $this->Session->setFlash('Invalid movie');
    39. $this->redirect(array('action' => 'index'));
    40. }
    41. if (!emptyempty($this->data)) {
    42. if ($this->Demomovie->save($this->data)) {
    43. $this->Session->setFlash('The movie has been saved');
    44. $this->redirect(array('action' => 'index'));
    45. } else {
    46. $this->Session->setFlash('The movie could not be saved. Please, try again.');
    47. }
    48. }
    49. if (emptyempty($this->data)) {
    50. $this->data = $this->Demomovie->read(null, $id);
    51. }
    52. }
    53. function view($id = null) {
    54. if (!$id) {
    55. $this->Session->setFlash('Invalid movie');
    56. $this->redirect(array('action' => 'index'));
    57. }
    58. $this->set('movie', $this->Demomovie->findById($id));
    59. }
    60. }
    61. ?>
    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: