tags:

views:

360

answers:

4

Hello

I have an existing PHP/MySQL application that I want to convert over to CakePHP. Unfortuaintly I can not change the column names and they do not follow the CakePHP standard.

For example: One of the table names is called "user" and its not plural. The primary key is called "user_id" instead of "id". The creation date is called 'generated' instead of "created", ect...

I was hoping there would be a way to define aliases for table names and fields in the CakePHP model but I can't seem to find it. Any suggestions?

Thank you for your time

+4  A: 

You can change the name of the table associated with a model, as well as the primary key:

class User extends AppModel {
    var $table = 'User';
    var $primaryKey = 'id';
}

As far as the "generated" thing, I haven't found a way to override the "created/modified" defaults in CakePHP (other than going through the library source and hacking it directly). Are you able to add columns?

inkedmn
Yes, I can add columns I would prefer not to thou. I will try you solution tonight. Thanks for the advice.
Steven smethurst
+3  A: 

About "generated" you can insert/update values with callbacks like beforeSave.

<?php
class User extends AppModel {
    var $table = 'user';
    var $primaryKey = 'user_id'

    function beforeSave() {
        this->data['generated'] = date('Y-m-d H:i:s');
    }
}
?>
Davi Kenji
+1  A: 

There is an AliasBehavior but it has certain limitations (see comments on that article).

Other than that, some basic customisation is in order:

As far as I know, there is currently no way to tell cake your created field is called generated, you'll probably just have to handle that manually, possibly in Model::beforeSave().

dr Hannibal Lecter
+1  A: 

CakePHP lets you specify anything that is different from the standard conventions it promotes. Being the egomaniac shill that I am, I can recommend a book about refactoring applications using CakePHP that I wrote.

http://www.easyphpwebsites.com/books/v/Refactoring-Legacy-Applications-Using-CakePHP

Cheap $13 PDF.

GrumpyCanuck