CakePHP:在仅存储user_id的BelongsTo中显示user_name
我认为这是一个常见问题,所以如果这很烦人,我提前道歉。
我有一些表关系设置,并且希望在拉出包含 BelongsTo 条目的相关表时显示 user_name 而不是 user_id。
有没有一种简单的方法可以在这些名称自己的表中查询这组关系?
特别是,在显示项目列表时,我还显示相关状态列表。 STATUSES 与 USERS 和 PROJECTS 都有 BelongsTo 关系,我想显示 user_name 和 project_name 而不是 _id。
我当前将项目的控制器设置为递归:
$this->Project->recursive = 2;
因此它允许我调用与项目相关的状态:
<?php echo $status['user_id'];?>
但是,这是我更喜欢显示用户名的地方。
有什么想法吗?
I assume this is a common question so my apologies in advanced if this is at all annoying.
I have a few table relationships setup and would like to show the user_name rather than user_id when pulling up a related table which contains BelongsTo entries.
Is there a simple way to do a query for these names in their own tables for this set of relationships?
In particular, while displaying a list of PROJECTS, I am also displaying a list of related STATUSES. STATUSES have a BelongsTo relationship with both USERS and PROJECTS and I'd like to display the user_name and project_name rather than _id.
I am currently setting the PROJECT's controller to being recursive:
$this->Project->recursive = 2;
So it allows me to make calls to the STATUSES' related to the PROJECTS:
<?php echo $status['user_id'];?>
However, this is where I'd prefer to display user_name.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在用户模型中:
这会影响 find('list') 和脚手架。
要在第二个示例中显示 user_name,只需将其更改为:
In User model:
That would affect find('list') and scaffolding.
To display user_name in your second example, simply change it to: