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.ctp, edit.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 inser updata and delete can do but if you want to validation the create 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:)
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.ctp, edit.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 inser updata and delete can do but if you want to validation the create 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:)
Comments
Post a Comment