开发者

CakePHP one to many Combo box

开发者 https://www.devze.com 2023-01-11 19:58 出处:网络
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.

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 me开发者_Go百科thods to show the name of city instead of the id but I've failed.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号