*** Files for autogenerated Bake using a simple Post table of ID, Title, Body, Date and Time Created
mysql> use bake_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_bake_db |
+-------------------+
| posts |
+-------------------+
1 row in set (0.00 sec)
Here is the file to generate the tables:
$ cat /var/www/cakephp/app/config/createdb.sql
/* First, create our posts table: */
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
/* Then insert some posts for testing: */
INSERT INTO posts (title,body,created)
VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title,body,created)
VALUES ('A title once again', 'And the post body follows.', NOW());
INSERT INTO posts (title,body,created)
VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
All files are under the /www/var/cakephp/app directory.
This is in the ../posts/views directory.
**************************************** add.ctp *****************
**************************************** add_post.ctp **************
This is just a file that exists in name but is zero bytes. i.e.
-rw-r--r-- 1 root root 0 2009-10-16 15:19 add_post.ctp
You can create it with the command:
touch add_post.ctp
in the directory all the other files reside for the views.
**************************************** edit.ctp *****************
create('Post');?>
end('Submit');?>
link(__('Delete', true), array('action'=>'delete', $form->value('Post.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('Post.id'))); ?>
counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
));
?>
sort('id');?>
sort('title');?>
sort('body');?>
sort('created');?>
sort('modified');?>
>
link(__('View', true), array('action'=>'view', $post['Post']['id'])); ?>
link(__('Edit', true), array('action'=>'edit', $post['Post']['id'])); ?>
link(__('Delete', true), array('action'=>'delete', $post['Post']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $post['Post']['id'])); ?>
************************************************************************
Here is the controllers file in : /var/www/cakephp/app/controllers
posts_controller.php
Post->recursive = 0;
$this->set('posts', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Post.', true));
$this->redirect(array('action'=>'index'));
}
$this->set('post', $this->Post->read(null, $id));
}
function add() {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The Post has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
}
}
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Post', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The Post has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Post->read(null, $id);
}
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for Post', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Post->del($id)) {
$this->Session->setFlash(__('Post deleted', true));
$this->redirect(array('action'=>'index'));
}
}
}
?>
******************************************************************************************************
This is the ../app/models/post.php
array('numeric'),
'title' => array('notempty'),
'body' => array('notempty')
);
}
?>
****************************************************
Note, I have not included the core.php or the routes.php files because they are identical to the ones I used
when I wrote the cakephp script by hand.