Scaffolding in CakePHP

. Monday, June 9, 2008
  • Agregar a Technorati
  • Agregar a Del.icio.us
  • Agregar a DiggIt!
  • Agregar a Yahoo!
  • Agregar a Google
  • Agregar a Meneame
  • Agregar a Furl
  • Agregar a Reddit
  • Agregar a Magnolia
  • Agregar a Blinklist
  • Agregar a Blogmarks

What is scaffolding?
Its something that you will find very useful in production apps. When you begin, you may want to throw up stuff real quick in order to get started. Ultimately, its a built in system for adding, editing, viewing and deleting your database records quickly.

Database table
Suppose we want to work with a table named users, and here is its SQL structure

CREATE TABLE `users` (
`id` bigint(20) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(96) NOT NULL,
`password` varchar(50) NOT NULL,
`updated` int(11) NOT NULL,
`created` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
)

Model
Now lets create a model file first. It should be placed here: /app/models/user.php
<?
class User extends AppModel
{
var $name = "User";
var $uses = "users" // table name
}
?>


Controller
Create a controller file here: /app/controllers/users_controller.php
<?
class UsersController extends AppController
{
var $name = 'Users';
var $scaffold;
}
?>

View
Not required since the views for scaffolding are generated by CakePHP automatically.

Visit http://yoursite.com/users/ and start scaffolding.
Note: Always visit yoursite.com/controllerName/ to see the visual output of your controller.

I will show you a user registration and logging system in CakePHP soon.

9 comments:

Anonymous said...

hope to read your next cakephp article on user management. i am still struggling with this MVC thing.

Anonymous said...

Hi i got problems when I try to see the result: 404 page not found. I'm using cake on kubuntu+apache, I can see cake/index.php but I guess I got some permission not working...any idea?

Fahad Ibnay Heylaal said...

it seems u dont have mod_rewrite enabled in your apache. try installing that module.

Anonymous said...

well i think problem should come from apache mod rewrite, but when I print:

sudo a2enmod rewrite

it says the module is already enabled!

does it mean mod_rewrite works, right?

Fahad Ibnay Heylaal said...

open httpd.conf in a text editor. and search for 'mod_rewrite'. make sure that the line is uncommented (without a hash - #). this will enable rewrite module for apache.

Anonymous said...

Hey I set:

AllowOverride all

in my webroot and it worked (many tnx!).

Still i get this warning:

Warning: file_put_contents(/var/www/cake/app/tmp/cache/models/default_emicake_list) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/cake/cake/basics.php on line 936

Warning: file_put_contents(/var/www/cake/app/tmp/cache/models/default_users) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/cake/cake/basics.php on line 936

Fahad Ibnay Heylaal said...

you are getting that error message because your 'tmp' folder is not writable. cakephp caches your database tables (not records) for faster performance.

Anonymous said...

Tnx Fahad, so where do I have to modify tmp folder to make it writable? Do I have to search in Apache config files?

Scaffolding said...

Emiliano, if you are on OS X or Unix you can use CHMOD to make the tmp folder writable. To do this, you need to load up Terminal (on a mac) or log in via SSH. Next, for example, you can do:

chmod -R 777 /path/to/your/site/tmp