I have just started working with CakePHP, so I am a complete newbie with working in this framework and have taken on a legacy system with a pretty awful database structure. I went in and added primary keys to the tables that didn't have them. However, I can't find information on where to map columns in the model, and could use some links so I can get oriented in this area. I'm starting with their user table, which has been named "system". I created a model called User:
class User extends AppModel {
var $name = 'User';
var $useTable = 'system';
}
I want to map the columns to property names that are more in line with conventions without changing the existing database, since they are currently using that table. The table structure is:
Table: system
Field Name DataType Nullable Default
------------------------------------------------------------------
user_id tinyint(4) No None AUTO_INCREMENT
s_rec_type tinyint(4) Yes NULL
s_user_id varchar(20) Yes NULL
s_init char(2) Yes NULL
s_password varchar(12) Yes NULL
admin_access tinyint(4) No 0
fname varchar(50) No
lname varchar(50) No
email_address varchar(50) Yes NULL
created datetime Yes NULL
modified datetime Yes NULL
// I added email_address, created, and modified.
I want to map the columns in the model as follows:
user_id -> Id
s_rec_type -> UserType
s_user_id -> UserName
s_init -> Initials
s_password -> Password
admin_access -> AdminAccess
fname -> FirstName
lname -> LastName
email_address -> EmailAddress
created -> Created
modified -> Modified
so that I can use the methods like $this->User->findAllById($id) or $this->User->findByInitials('sb') or access the id by $this->User->id instead of something like $this->User->user_id and be more consistent with naming conventions with code that is easier to understand.
This table will be used also to determine what menu options users have access to and which pages the user can access. I'll deal with that later.
I would take a look at virtual fields. Your model attribute initialization might look like this:
var virtualFields = array (
'UserType' => 's_rec_type',
'UserName' => 's_user_id',
'Initials' => 's_init',
.....
'Modified' => 'modified'
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With