Thursday, September 13, 2012

simple example add , delete ,update ,show records in cakephp

stp-1
CREATE TABLE IF NOT EXISTS `movies` (
  `id` char(36) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `genre` varchar(45) DEFAULT NULL,
  `rating` varchar(45) DEFAULT NULL,
  `format` varchar(45) DEFAULT NULL,
  `length` varchar(45) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
----------------------------------------------------------------------------------------------------------
stp- 2->create controller page under(moviescontroller.php)-> D:\xampp\htdocs\cakephp_lt\app\Controller\moviescontroller.php and paste below code
----------------------------------------------------------------------------------------------------------
<?php
class MoviesController extends AppController {
    public $components = array('Session');
   
   
        public function index()
        {
            $movies = $this->Movie->find('all');
            $this->set('movies', $movies);
         }
   
            public function add()
             {
                    if (!empty($this->data)) {
                    $this->Movie->create($this->data);
                    if ($this->Movie->save()) {
                    $this->Session->setFlash('The movie has been saved');
                    $this->redirect(array('action' => 'add'));
                    } else {
                    $this->Session->setFlash
                    ('The movie could not be saved. Please, try again.');
                    }
                    }
            }
               
           
            public function delete($id = null)
             {
                if (!$id) {
                $this->Session->setFlash('Invalid id for movie');
                $this->redirect(array('action' => 'index'));
                }
                if ($this->Movie->delete($id)) {
                $this->Session->setFlash('Movie deleted');
                } else {
                $this->Session->setFlash(__('Movie was not deleted',
                true));
                }
                $this->redirect(array('action' => 'index'));
             }



            function edit($id = null) {
                    if (!$id && empty($this->data)) {
                        $this->Session->setFlash('Invalid movie');
                        $this->redirect(array('action' => 'index'));
                    }
                    if (!empty($this->data)) {
                        if ($this->Movie->save($this->data)) {
                            $this->Session->setFlash('The movie has been saved');
                            $this->redirect(array('action' => 'index'));
                        } else {
                            $this->Session->setFlash('The movie could not be saved. Please, try again.');
                        }
                    }
                    if (empty($this->data)) {
                        $this->data = $this->Movie->read(null, $id);
                    }
                }
               
    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash('Invalid movie');
            $this->redirect(array('action' => 'index'));
        }
        $this->set('movie', $this->Movie->findById($id));
    }
}
?>
-------------------------------------------------------------------------------------------------------------
create view file for show ,add,edit,delete under D:\xampp\htdocs\cakephp_lt\app\View\movies\(add.ctpedit.ctp,index.ctp,view.ctp)
------------------------------------------------------------------------------------------------------------

add.ctp
------------------------------------
<div class="movies form">
<?php
echo $this->Form->create('Movie');
echo $this->Form->inputs(array('title', 'genre', 'rating', 'format', 'length'));
echo $this->Form->end('Add');
?>
</div>

<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link('List of bikash store', array('action' => 'index'));?></li>
    </ul>
</div>
----------------------------------
edit.ctp
--------------------------------
<div class="movies form">
<?php
echo $this->Form->create('Movie');
echo $this->Form->inputs(array('id', 'title', 'genre', 'rating', 'format', 'length'));
echo $this->Form->end('Edit');
?>
</div>
<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link('List Movies',
array('action' => 'index'));?></li>
    </ul>
</div>
----------------------------------
index.ctp
--------------------------------
<div class="movies index">
    <h2>Movies</h2>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Rating</th>
            <th>Format</th>
            <th>Length</th>
            <th class="actions">Actions</th>
        </tr>
    <?php foreach ($movies as $movie): ?>
    <tr>
        <td><?php echo $movie['Movie']['title']; ?> </td>
        <td><?php echo $movie['Movie']['genre']; ?> </td>
        <td><?php echo $movie['Movie']['rating']; ?> </td>
        <td><?php echo $movie['Movie']['format']; ?> </td>
        <td><?php echo $movie['Movie']['length']; ?> </td>
        <td class="actions">
            <?php echo $this->Html->link('View',
array('action' => 'view', $movie['Movie']['id'])); ?>
            <?php echo $this->Html->link('Edit',
array('action' => 'edit', $movie['Movie']['id'])); ?>
            <?php echo $this->Html->link('Delete',
array('action' => 'delete', $movie['Movie']['id']),
null, sprintf('Are you sure you want to delete %s?', $movie['Movie']['title'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>
    </table>
</div>
<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link('New Movie', array('action' => 'add')); ?></li>
    </ul>
</div>

----------------------------------
view.ctp
--------------------------------
<div class="movies view">
<h2>Movie</h2>
    <dl>
        <dt>Title</dt>
        <dd><?php echo $movie['Movie']['title']; ?></dd>
        <dt>Genre</dt>
        <dd><?php echo $movie['Movie']['genre']; ?></dd>
        <dt>Rating</dt>
        <dd><?php echo $movie['Movie']['rating']; ?></dd>
        <dt>Format</dt>
        <dd><?php echo $movie['Movie']['format']; ?></dd>
        <dt>Length</dt>
        <dd><?php echo $movie['Movie']['length']; ?></dd>
        <dt>Created</dt>
        <dd><?php echo $movie['Movie']['created']; ?></dd>
        <dt>Modified</dt>
        <dd><?php echo $movie['Movie']['modified']; ?></dd>
    </dl>
</div>


<div class="actions">
    <h3>Actions</h3>
    <ul>
        <li><?php echo $this->Html->link
('Edit Movie', array('action' => 'edit', $movie['Movie']['id'])); ?> </li>
        <li><?php echo $this->Html->link
('Delete Movie', array('action' => 'delete', $movie['Movie']['id']),
null, sprintf('Are you sure you want to delete # %s?', $movie['Movie']['id'])); ?> </li>
        <li><?php echo $this->Html->link
('List Movies', array('action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link
('New Movie', array('action' => 'add')); ?> </li>
    </ul>
</div>
----------------------------------------------------------------------
then you can also insert, updata and delete can do but if you want to validation your form fields then you have to create e a model to validatae
-------------------------------------------------------------------------
create model uder->D:\xampp\htdocs\cakephp_lt\app\Model\(movie.php)
------------------------------------------------------------------------------
you can validate for your fields using model paste below these code
----------------------------------------------
<?php
class Movie extends AppModel {

   var $name = 'movie';  
    var $validate =array(
    'title' =>array(
               'alphaNumeric' =>array(
               'rule' => array('minLength',2),
               'required' => true,
               'message' => 'Enter should be minimum 2 only')              
               ),
    'genre' =>array(
               'alphaNumeric' =>array(
               'rule' => array('minLength',4),
               'required' => true,
               'message' => 'Enter should be minimum 4 only')              
               ),
   
           
    'rating' =>array(
               'alphaNumeric' =>array(
               'rule' => array('minLength',2),
               'required' => true,
               'message' => 'Enter should be minimum 4 only')              
               )
   
            );
  
}
?>

i hope every one can easily understand please:)

33 comments:

  1. its really help full thanks for this ...

    ReplyDelete
  2. Thank for your appreciation, i always here if you have any question related cakephp or other technology you can ask by comment.

    thanks

    ReplyDelete
  3. Hi,
    I am new in cakephp , this article is very helpful for me. I want to learn more can u help me .

    ReplyDelete
  4. Hi Asis,
    if you facing any problem i`m available any time through comment.
    Thanks again for help full

    ReplyDelete
  5. http://tuxradar.com/content/cakephp-tutorial-build-web-apps-faster

    this link are totally this code ....

    ReplyDelete
  6. Hi Patel,
    exactly you can see our post. we are just testing all those programing after it successfully execute. then we could uploaded for help to other beginner.

    ReplyDelete
    Replies
    1. ya i can see you post it's good and it's really works ..

      Delete
  7. http://tuxradar.com/content/cakephp-tutorial-build-web-apps-faster

    i' m just inform you .. this link are copy of your link

    ReplyDelete
  8. ok Paltel, thanks for your information. we are always resolved the problem.if you have any question you can asked by comment.

    thanks

    ReplyDelete
  9. thanks .

    I prefer learn from examples rather then digging into docs. However docs are fruitful but you have provide me cheese.thanks again.


    ReplyDelete
    Replies
    1. hello, thanks for your commenting ...............we always happy, any one helpful from our tutorial ...:)

      Delete
  10. thanks
    Its very helpful for me
    using this post I can do many other work in cakephp .

    ReplyDelete
  11. Hi, How can I use a different primary key id? especially in update and insert..
    Thank you in adance.

    ReplyDelete
    Replies
    1. Hi ,
      you mean you want to pass multiple different query string like
      :$movie['Movie']['id'] and other primary key?

      Delete
    2. Nom in my table I have a different different name than id for example: PERSON_CODE
      I want to create a "create new" and "edit" forms. I have declared it in the modal using
      $primarykey= "PERSON_CODE"
      but still no use. do you have a solution?

      Delete
    3. You need to change your primarykey fields according in model ,controller, and presentation page. could you provide your code i can assist to correction

      Delete
  12. thanxs
    Its very helpful for me

    ReplyDelete
  13. i have a simple html code that i would oike to ask for help.. pls if anyone here can help me and i will really appreciate your help.
    just need a simple html code to make a table
    that i can search , add edit and delete.. pls
    if anyone can help me with a good heart
    pls email me at
    aaron_mendoza81@yahoo.com
    or here

    ReplyDelete
  14. Hi Eiron,

    i really appreciate to help you , you would need to use PHP scripting language for managing these operation .if you send your HTML code to my email id "nayakr.bikash@gmail.com" i can do using PHP langunge for your functionality.

    let me know anything you need..... :)

    ReplyDelete
    Replies
    1. hi sir.. i sent email to you..pls help me with this little project..
      i want to learn php and html ..basic only pls...

      Delete
    2. Ok fine , i will let you know............:)

      Delete
  15. Undefined variable: movies [APP/Template\movies\index.ctp, line 12]
    how can i resolve?

    ReplyDelete
  16. best and very simple tutorial & website

    ReplyDelete
  17. This is a clear and useful post.we can also try phpcodebuilder for creating crud with our table name and fields dynamically.http://phpcodebuilder.com/crud-generator

    ReplyDelete