CakePHP 一对多组合框
嘿,我有两个模型 User 和 City,其中一个用户属于一个城市,一个城市有很多用户。 在城市表上我有 city_id 和 city_name。
我已经使用过 Cake Baking 并创建了用户模型,当我添加新用户时,城市字段下的组合框显示城市的 ID 而不是名称。
我尝试了多种方法来显示城市名称而不是 ID,但都失败了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有提及您尝试过的内容,但通常您会使用模型的
displayName
属性来设置应用作记录“标题”的字段。对于您的情况,我认为:请注意,在 1.3 中,属性名称看起来是
displayField
而不是displayName
。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:Note that in 1.3, it looks like the attribute name is
displayField
rather thandisplayName
.Cake 需要特定的表格布局才能使自动化工作。罗布解释了如何解决这个问题。但是,如果您想避免编写额外的代码行,则应该考虑相应地组织表格。
当您烘焙此模型时,Cake 将自动使用
name
作为显示名称。这将适用于您创建的每个表。此外,如果您对表使用id
,它会自动知道如何将其作为外键从其他表中引用为city_id
。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.
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 useid
for the table, it will automatically know how to reference it as a foreign key from other tables ascity_id
.