tags:

views:

47

answers:

2

Hey I have two models User and City, where a user belongs to a city and a city has many users. On the city table I have city_id and city_name.

I've used used cake bake and created the user model and when I add a new user the combos box under the city field is showing the id of the city instead of the name.

I've tried several methods to show the name of city instead of the id but I've failed.

+1  A: 

You don't mention what you've tried, but usually you'd use the model's displayName property to set the field that should be used a record's "title". In your case, I'd think:

$this->displayName = 'city_name';

Note that in 1.3, it looks like the attribute name is displayField rather than displayName.

Rob Wilkerson
CakePHP will use 'title' by default, assuming such a field is present, and $this->displayField is not defined.
Wayne Khan
+1  A: 

Cake requires a specific table layout for the automation to work. Rob explained how to get around it. But if you want to avoid writing the extra lines of code, you should consider organizing your tables accordingly.

CREATE TABLE `cities` (
  `id` int(11) AUTO_INCREMENT NOT NULL,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY (`id`)
)

When you bake this model, Cake will automatically use name as the display name. This will apply to every table you create. In addition, if you use id for the table, it will automatically know how to reference it as a foreign key from other tables as city_id.

cdburgess