Thursday, November 12, 2009

How to develop a PHP Application on the FLY Application scaffolding with CakePHP

Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects.
CakePHP allows application scaffolding on the fly. With this technology we can develop a workable application on the fly.
We have already created 'add', 'edit', 'delete', and 'view' methods for 'posts' table.
Let us create another table, 'comments'.

SQL:


CREATE TABLE `comments` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` char(50) NOT NULL,
  `body` text NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY  (`id`)
)

Now create the model file.
Step:
1. Create a new file.
2. Copy-paste following lines:
<?php
class Comment extends AppModel {
var $name = 'Comment';
}
?>
3. Save the file as '/app/models/comment.php

Now create the controller file.
1. Create a new file.
2. Copy-paste following lines:

<?php
class CommentsController extends AppController {

var $name = 'Comments';
var $scaffold;
}
?>

3. Save the file as '/app/controllers/comments_controller.php'

Note:
Please note  $scaffold variable. It tells Cake to create a workable application on the fly.
And that's it.

Now point your browser to:
http://caketest.local/comments

You can see a full featured comments application with 'add', 'view', 'edit', 'delete' option.

Here is a screenshot:
In my next post, I'll try to demonstrate how can you fine-tune your scaffolded application.

Cheers!

1 comment:

Jippi said...

Hi

If you want to play around with scaffolding & CRUD operations, but in a much more flexible way thank bake and CakePHP scaffolding - and one that can be used in production too - check out my CakePHP Scaffolding / Crud plugin here:

https://github.com/jippi/cakephp-crud