Create Database Table
CREATE
TABLE
`categories` (
`id`
INTEGER
(11)
NOT
NULL
AUTO_INCREMENT,
`
name
`
VARCHAR
(255)
NOT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=MyISAM
Create Model app\models\category.php
1 2 3 4 5 | <?php class Category extends AppModel { var $name = 'Category' ; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php class CategoriesController extends AppController { var $name = 'Categories' ; function index() { $ this ->set( 'categories' , $ this ->Category->find( 'all' )); } function add() { if (!empty($ this ->data)) { if ($ this ->Category->save($ this ->data)) { $ this ->Session->setFlash( 'Your category has been saved.' ); $ this ->redirect(array( 'action' => 'index' )); } } } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!-- File: /app/views/categories/index.ctp --> <?php echo $html->link( 'Add Category' ,array( 'controller' => 'categories' , 'action' => 'add' )); ?> <h1>Categories</h1> <table> <tr> <th>Id</th> <th>Title</th> </tr> <?php foreach ($categories as $category): ?> <tr> <td><?php echo $category[ 'Category' ][ 'id' ]; ?></td> <td> <?php echo $html->link($category[ 'Category' ][ 'name' ], array( 'controller' => 'categories' , 'action' => 'view' , $category[ 'Category' ][ 'id' ])); ?> </td> </tr> <?php endforeach; ?> </table> |
1 2 3 4 5 6 7 | <!-- File: /app/views/categories/add.ctp --> <h1>Add Category</h1> <?php echo $form->create( 'Category' ); echo $form->input( 'name' ); echo $form->end( 'Save Post' ); ?> |