Bookcafe

Monday, March 8, 2010

Simple CakePhp Application - Example

There are about three steps in doing this:

Step 1. Create a datebase and tables and dont forget to insert data into the tables->The name for the tables must be in plural form.

Step 2. Create a php file in app/models directory

- for example syarikat.php->the name of the file must be in singular form

- the code:

class Syarikat extends AppModel
{
var $name = 'Syarikat';
}
?>

Step 3. Create a php file in app/controllers

- for example syarikats_controller.php ->the file name must be filenameplural_controller.php

- the code:

class SyarikatsController extends AppController
{
var $name = 'Syarikats';
var $scaffold;
}
?>

Note: the scaffold variable is use when there is no app/view .thtml file.

The scaffold variable automatically create index.thtml, add.thtml, view.thtml and delete.thtml

If you need to create your own view, process or layout, you can ommit the scaffold variable.

To access your application from browser (in my case)

type at your browser:
http://localhost/cakephp/syarikats

If your get this error (in my case):

Notice (8): Undefined index: id

Then, you must name your first field in the table as ID and make it auto increment.
Example when creating your database
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY

The view of my first application from the browser



Of course you can customised this view according to your needs. For that, I will show to you all in the near future.

No comments:

Post a Comment