CakePHP: Display user_name in BelongsTo where only user_id is stored
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 开发者_StackOverflow社区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?
In User model:
var $displayField = 'user_name';
That would affect find('list') and scaffolding.
To display user_name in your second example, simply change it to:
<?php echo $status['user_name'];?>
精彩评论