This blog is about the PHP solutions I have to think of when I'm developing systems almost every single day...

Thursday, April 24, 2008

Advanced routing in CakePHP

Wow.. routing in Cakephp is pretty good. Works both ways. To learn more go to:
http://debuggable.com/posts/new-router-goodies:480f4dd6-4d40-4405-908d-4cd7cbdd56cb

The most important part for me in the post was the past variable. Finally I'd be able to pass variables to the function. So for something like:


Router::connect('/:committee/:controller/:action/:id',array(),array('pass'=>array('committee','id')));

The empty array means you want to just pass to the default controller and action you keyed into the url. The pass array means you want to pass the committee as the first variable of the action and id as the second variable. Pretty cool stuff.

Wednesday, April 23, 2008

I need to migrate

Fuh.. I need to get out of this country.. :P

Anyway.. that may be a long time coming. But first here's a good reference on how to migrate databases using cakephp at:
http://cakebaker.42dh.com/2008/04/13/migrations-the-cakephp-way/

To sum it up you have to follow these steps:


  1. Update the tables in sql
    You can use phpmyadmin, mysql console or anything you want

  2. Create/Update your models
    Make sure your models are created for each table you add. Easy using `cake bake model`

  3. Update the schema
    Run `cake schema generate`. This will create the app/config/sql/schema.php file. This file is needed for others to compare with.

  4. Commit your changes and others update theirs
    Use whatever you use, svn, cvs, git, mercurial. As long as everyone get the new version of app/config/sql/schema.php

  5. Update your database
    Just run `cake schema run update`. And walah.. you've got your database all nice and update.



Have I said cakephp is cool.. Oh yeah, it sure is... :)

Wednesday, April 2, 2008

Using components in shell class

One of the great things about cake is it's shell functions. It makes it easy to write shell scripts for your app. First step is to create the file cake/console/libs. Lets name this file myapp.php. Then inside you would do:


class MyappShell extends Shell {
function main(){
.....
}
}

Do whatever you want inside that class. You can even define the $uses array in the class to use whatever models your app have. And if you want to use a certain component, lets say the Auth component, you'd do:

App::import('Component','Auth');
$this->Auth=& new AuthComponent(null);

Then you can use stuff like $this->Auth->password and such. Great stuff this cakephp.. :)

Bake it quick

Even though I've been using cakephp for over 2 weeks already, but I've only just discovered that you can actually bake very quickly just by typing:


./cake bake all

in your cake/console directory. It will create your model, controls and views all in one shot. But only for one model. But it's much easier than how I usually do it (through the menus).