In this post we are going to add a ‘users’ table to our MySQL schema ‘aa01′ and create a data model, a controller and some views.
Currently, if we load the URL http://localhost/aa01/index.php a Welcome page is displayed and the messages are mainly green, signifying all is well with our ‘aa01′ project. Now, if we type http://localhost/aa01/index.php/users or http://localhost/aa01/index.php/login, all hell breaks loose, the messages turn red, signifying not so well. Read messages carefully as they show a lot of info.
Users table
Let’s add the users table. Get into MySQL and run the following script. The minimum fields we need are id, username and password.
DROP TABLE IF EXISTS `aa01`.`users`; CREATE TABLE `aa01`.`users` ( `id` int(10) unsigned NOT NULL auto_increment, `username` varchar(45) NOT NULL, `firstname` varchar(45) default NULL, `lastname` varchar(45) default NULL, `active` tinyint(1) NOT NULL default '0', `email` varchar(100) default NULL, `password` varchar(40) default NULL, `randomsalt` varchar(40) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
While you’re in MySQL, add a couple of users like Joe, password ‘joe08′ and Jane with a password of ‘jane08′ just for testing.
Bake Model
Back to our staple comand line. Run cake bake -app aa01 again. A console command line shows asking what next.
Select ‘M’ for model, ‘1′ for the User model. Enter validation ‘alphanumeric’ for username and password. Skip model associations and press OK. The file \aa01\models\user.php will be created.
Note that the Bake console times out after 300 seconds, so if you take too long, you have to start again. Also action any requests for test file generation, so in the future you might test your project.
Bake Controller
Back to our staple comand line. Run cake bake -app aa01 again. A console command line shows asking what next.
Select ‘C’ for controller, ‘1′ for the Users model. Select ‘Interactively’, skip ‘Scaffolding’, include basic class methods. Skip admin routing, other helpers, components. Yes to using Sessions and press OK. The file \aa01\controllers\users_controller.php will be created.
Bake Views
Last in this baking series is to bake the simple views Index, Add, Delete and View. Back to the trusty bake command.
Select ‘V’ for Views, ‘1′ for Users model, Interactively, Yes to scaffolded basic views, No to admin routing, OK to create.
Some local tests
Try the URLs below;
http://localhost/aa01/index.php - Shows Welcome page
http://localhost/aa01/index.php/users – Lists users
http://localhost/aa01/index.php/users/add- Add a user
http://localhost/aa01/index.php/users/delete – fails to delete, lists users
http://localhost/aa01/index.php/users/list – fails
http://localhost/aa01/index.php/users/edit – fails to edit, lists users
http://localhost/aa01/index.php/users/view – fails to view, lists users
http://localhost/aa01/index.php/users/login – fails
http://localhost/aa01/index.php/users/logout – fails
http://localhost/aa01/index.php/users/register – fails
Almost finished
Now we have users loaded in the database, a basic working front end to our project (except login / logout / register) where we can add / delete / edit / list users. Go on, try adding, editing and deleting some users. Try silly things like submitting the add form with no username, watch for error messages.
The whole CakePHP process was simple after all, wasn’t it?
Next post is login / logout /register.