CRUD in CakePHP Part : 2
In previous post we learnt about view and listing of the Users.In this post we'll learn about add,edit and delete operations.
Adding Users
To add a user record put the below function in our UsersController.php
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('User has been saved.'));
$this->redirect(array('action' => 'index'));
} else { $this->Session->setFlash(__('Unable to add User.')); } } }
Here we have to include the SessionComponent and SessionHelper in our controller.
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
Now its time to create view file for the add.So create a file add.ctp in /app/View/Users/
Our add view looks like.
<!-- File: /app/View/Users/add.ctp -->
<h1>Add User</h1>
<?php
echo $this->Form->create('User');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('email');
echo $this->Form->input('message', array('rows' => '3'));
echo $this->Form->end('Save User');
?>
We used the FormHelper to generate the opening tag for an HTML form. $this->Form->create() will generates:
<form id="UserAddForm" method="post" action="/users/add">
The $this->Form->end() generates a submit button.
Go back to update our /app/View/Users/index.ctp view file for adding “Add User” link. Before the <table>, simply add the following line:
<?php echo $this->Html->link(
'Add User',
array('controller' => 'users', 'action' => 'add')
); ?>
Editing Users
Create a new edit() method in UsersController.php
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid user'));
}
$user = $this->User->findById($id);
if (!$user) {
throw new NotFoundException(__('Invalid user')); } if ($this->request->is('post') || $this->request->is('put')) { $this->User->id = $id; if ($this->User->save($this->request->data)) { $this->Session->setFlash(__('User has been updated.')); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('Unable to update User.')); } } if (!$this->request->data) { $this->request->data = $user; }
}
Create a view file for the edit method.
<!-- File: /app/View/Users/edit.ctp -->
<h1>Edit User</h1>
<?php
echo $this->Form->create('User');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('email'); echo $this->Form->input('message', array('rows' => '3')); echo $this->Form->input('id', array('type' => 'hidden')); echo $this->Form->end('Update User');?>
One thing to note here: CakePHP will assume that you are trying to edit a model if the ‘id’ field is present in the data array. If no ‘id’ is present, Cake will assume that you are trying to insert a new model when save() is called.
To edit specific users, update your index view.
<?php echo $this->Html->link(
'Add User',
array('controller' => 'users', 'action' => 'add')
); ?>
<h1>Blog Users</h1>
<table> <tr> <th>Id</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Message</th> <th>Action</th> </tr> <!-- Here is where we loop through our $users array, printing out user info -->
<?php foreach ($users as $user): ?> <tr> <td><?php echo $user['User']['id']; ?></td> <td><?php echo $this->Html->link($user['User']['firstname'],array('controller' => 'users', 'action' => 'view', $user['User']['id'])); ?></td> <td><?php echo $user['User']['lastname']; ?></td> <td><?php echo $user['User']['email']; ?></td> <td><?php echo $user['User']['message']; ?></td> <td><?php echo $this->Html->link('View', array('action' => 'view', $user['User']['id'])); ?> <?php echo $this->Html->link('Edit', array('action' => 'edit', $user['User']['id'])); ?> </td> </tr> <?php endforeach; ?> <?php unset($user); ?></table>
Deleting Users
Now create new function delete() in the UsersController:
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('The user having id: %s has been deleted.', $id));
$this->redirect(array('action' => 'index')); }}
Add delete link in index.ctp file
<td>
<?php echo $this->Form->postLink(
'Delete',array('action' => 'delete', $user['User']['id']),array('confirm' => 'Are you sure to delete?'));
?>
</td>
That's it..!! Your CRUD Application is Ready.Download Demo.
Download Demo
Comments
Post a Comment