CakePHP: Baking on Ubuntu
I have been upgrading an web application for a client. I initially
created the application using a custom php framework – spliced together
however awkwardly, but it worked. In version 2, I included YUI 2.8, but
have been using the same backend.
The site is nearly complete (ajax functionality, and database
development), I decided that I needed a more structured php framework. I
chose CakePHP primarily because I could bake
the models, views, and controllers. With only 17 tables, The
application is not very large, but the thought of creating models,
views, and controllers for 17 tables did not sound exciting.
I could not bake
on my Mac. I got an error, “Call to undefined function
mysql_connect()”. I searched for hours trying to figure out why I was
receiving the error, but to no avail. I think that I need to re-install
PHP. Not going to happen. I instead decided to setup Cake on my Ubuntu box (See my post: Setting Ubuntu For Web Development).
Profile Path Configuration
Before I could bake, I had to add it to my Path. First, I checked to see if it was already in the path:
I wasn’t there, so I added it:
echo 'export PATH=/home/les/public_html/cake/cake/console:$PATH' >> ~/.profile
|
Then I reloaded my profile:
PHP CLI
To test the path, I entered:
I received an error “exec: php: not found”, so I ran the command to find out where PHP was located on my Ubuntu box:
The command did not return anything. After some searching, I found
that I received the error because PHP CLI (command line interface) was
not installed on my Ubuntu box (although PHP is installed), so I
installed it:
sudo apt-get install php5-cli
|
After the installation, I ran:
which php
// returned /usr/bin/php
|
Now I needed to add PHP CLI to my path:
export PATH=/usr/bin/php:$PATH >> ~/.profile
|
The I ran the
cake command again:
You should see:
Welcome to CakePHP v1.2.4.8284 Console
---------------------------------------------------------------
Current Paths:
-app: les
-working: /home/les
-root: /home
-core: /home/les/public_html/cake
Changing Paths:
your working path should be the same as your application path
to change your path use the '-app' param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp
Available Shells:
APP/public_html/cake/vendors/shells:
- none
CORE/console/libs:
acl
api
bake
console
i18n
schema
testsuite
To run a command, type 'cake shell_name [args]'
To get help on a specific command, type 'cake shell_name help'
|
Baking
Since I already setup the database configuration (app/config/database.php) and created my MySQL tables, I entered:
//let cake know where your app folder is located
cake bake -app ~/public_html/cake/app/
|
I was then presented with the Interactive Bake Shell
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
>
|
Instead of creating each model, view, and controller individually, I am going to use the
bake all
command so that the MVC files can be created for each table. First,
I’ll enter ‘M’, so that cake will display the list of tables. This list
will be used so that I don’t have to refer back to my database while I’m
baking. After the table list displays, select ‘Q’ to quit.
Now I entered:
cake bake all -app ~/public_html/cake/app/
|
I was presented with the following:
Bake All
---------------------------------------------------------------
Possible Models based on your current database:
1. AppointSched
2. County
3. Country
4. CountyZip
5. Group
6. InfoService
7. InfoServicesCat
8. Language
9. MidInterp
10. OnsiteInterp
11. ReferTo
12. Referral
13. Requestor
14. Request
15. State
16. User
17. Widget
Enter a number from the list above, type in the name of another model, or 'q' to exit
[q] > 1
|
I selected 1 to have the AppointSched table baked. I received an error:
Notice: Schema generation error: invalid column type enum(‘H’,'O’)
does not exist in DBO in
/home/les/public_html/cake/cake/libs/model/schema.php on line 475
Apparently, CakePHP doesn’t like ENUM fields. I found a work-around,
so for now, I’m not concerned. Even without the work-around, I’m
assuming that I will be able to easily edit the model and change the
ENUM fields to strings. I think. I haven’t yet seen what cake bakes. I
have always created the models, views, and controllers manually.
Creating Controllers and Views
So, since the appoint_sched model was already created prior to
receiving the error, all I have to do is bake a controller and view:
cake bake -app ~/public_html/cake/app/
//displays
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> C
|
I select ‘C’ to bake the controller. I am then presented with a list
of controller names. I select AppointScheds. I am prompted with various
options.
---------------------------------------------------------------
Baking AppointSchedsController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] >
Would you like to use scaffolding? (y/n)
[n] > n
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
[n] >
Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
[n] > y
Please provide a comma separated list of the other helper names you'd like to use.
Example: 'Ajax, Javascript, Time'
> Ajax, Javascript
Would you like this controller to use any components? (y/n)
[n] > n
Would you like to use Sessions? (y/n)
[y] > y
|
It baked successfully. I repeated these steps to bake the View.
Shortcuts
To bake the remaining tables, I used baking shortcuts. Shortcuts
significantly reduce the amount of keystrokes. To bake with shortcuts,
enter
bake all and the table name.
After baking the remaining tables, it’s time to integrate YUI. Before integration, I first added authentication and access control lists to the application. Enjoy.