Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Add, Edit, Delete Examples In CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.13k
    Comment on it

    To perform add, delete, edit operation in Cake-PHP, first you need to create database table to store records.

    CREATE TABLE `users` (
      `id` int(10) UNSIGNED NOT NULL,
      `username` varchar(128) DEFAULT NULL,
      `password` varchar(128) DEFAULT NULL,
      `email` varchar(128) DEFAULT NULL,
    ) 
    

    Examples of CakePHP's Add, Edit, Delete action.

    Add Function: add() function is used to save data using the model (Suppose model name-> USER). If the request method is HTTP then delete action perform right task otherwise it will give error.

    public function add() 
        {
          if ($this->request->is('post')) 
            {
                $this->User->create();
                if ($this->User->save($this->request->data)) 
                {
                    $this->Session->setFlash(('The user has been saved.'));
                            return $this->redirect(array('controller' => 'Users', 'action' => 'index'));
                } else 
                {
                    $this->Session->setFlash(('The user could not be saved. Please, try again.'));
                }
            }
    }
    

    Edit Function: In edit(), first user have to pass $id parameter to access an existing record. if the ID have't pass or user does nor exit then, it will give an error.

    public function edit($id = null) {
            if (!$this->User->exists($id)) {
                throw new NotFoundException(_('Invalid user'));
            }
            if ($this->request->is(array('post', 'put'))) {
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(('The user has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(('The user could not be saved. Please, try again.'));
                }
            } else {
                $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
                $this->request->data = $this->User->find('first', $options);
            }
        }
    

    Delete Function: delete() function deletes the data which is specified by ID. After deleting the data, user would be redirected to index page.

    public function delete($id = null) {
            $this->User->id = $id;
            if (!$this->User->exists()) {
                throw new NotFoundException(('Invalid user'));
            }
            $this->request->allowMethod('post', 'delete');
            if ($this->User->delete()) {
                $this->Session->setFlash(('The user has been deleted.'));
            } else {
                $this->Session->setFlash(('The user could not be deleted. Please, try again.'));
            }
            return $this->redirect(array('action' => 'index'));
        }
    

    Index Function: indiex() action is used to fetch data from DB & showing it in front end using index.ctp file.

    public function index()
            {
                $users = $this->User->find('all');
                $this->set('users', $users);
             }
    

    Now create a view files (add,edit,index) for presentation under app\View\users(add.ctp, edit.ctp,index.ctp)

    index.ctp:

           

                <?php echo $this->Html->link('Edit', array('action' => 'edit', $user['User']['id'])); ?>
                <?php echo $this->Form->postLink('Delete', array('action' => 'delete', $user['User']['id'])),
            </td>
        </tr>
    <?php endforeach; ?>
        </tbody>
    
        </table>
    

    edit.ctp:

    <?php echo $this->Form->create('User'); ?>
        <?php echo ('Edit User'); ?>
        <?php
            echo $this->Form->input('id');
            echo $this->Form->input('username');
            echo $this->Form->input('password');
            ?>
        </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>
    

    add.ctp:

    <?php echo $this->Form->create('User'); ?>
        <?php echo __('Add User'); ?>
    
            echo $this->Form->input('username');
            echo $this->Form->input('password');
            echo $this->Form->input('email');
        ?>
    <?php echo $this->Form->end(__('Submit')); ?>
    

    check: login/logout in cakephp

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: