【发布时间】:2019-01-07 22:24:44
【问题描述】:
我是 CakePHP 和 PHP 的新开发人员。
我有 2 个表:
Tasks、Users和Customers。
除其他外,Tasks 有这个外键链接到 Users 表:Assigned_from 和外键 Customer_id 链接到 Customers 表。
所以我在UsersTable.php 中插入了这些行:
函数初始化:
$this->hasMany('TasksFrom', [
'foreignKey' => 'assigned_From',
'className' => 'Tasks'
]);
函数构建规则:
$rules->add($rules->existsIn(['assigned_from'], 'TasksFrom'));
相应地在 TasksTable.php 中: 函数初始化:
$this->belongsTo('UsersFrom', [
'foreignKey' => 'assigned_from',
'className' => 'Users'
]);
函数构建规则:
$rules->add($rules->existsIn(['assigned_from'], 'UsersFrom'));
在CustomersTable.php中:
函数初始化:
$this->hasMany('Tasks', [
'foreignKey' => 'customer_id'
]);
所以为了在用户视图中显示这些信息,我在 UsersController.php 中添加了这些行:
public function view($id = null)
{
$user = $this->Users->get($id,[
'contain' => ['TasksTo', 'TasksFrom', 'TasksBy']
]);
$this->set('user', $user);
}
如何修改我的代码以便在访问 echo $task->assigned_from 时显示用户名而不是用户 ID 以及在访问 echo $task->customer_id 时显示客户名而不是 Customer_id?
这是 users/view.ctp 中的当前代码:
foreach ($user->tasks_to as $task) {
?>
<tr id="<?php echo $task['id'] ?>">
<td><?php echo $task->priority ?></td>
<td><?php echo $task->name ?></td>
<td><?php echo $task->instructions ?></td>
<td><?php echo $task->assigned_from ?></td> //It displays user id, I want to display user name
<td><?php echo $task->customer_id ?></td> //Display Customer name instead of Customer_id
<td><?php echo $task->progress ?></td>
</tr>
<?php } ?>
调试输出 debug($user->tasks_to):
(int) 1 => object(App\Model\Entity\Task) {
'id' => (int) 2,
'name' => 'Design Logo for Website',
'task_type_id' => (int) 1,
'role_id' => (int) 5,
'instructions' => 'Design logo for website.com website, (instructions)',
'date_start' => object(Cake\I18n\FrozenDate) {
'time' => '2018-12-20T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'date_end' => object(Cake\I18n\FrozenDate) {
'time' => '2018-12-22T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'total_minutes' => (int) 120,
'assigned_to' => (int) 1,
'assigned_from' => (int) 1,
'customer_id' => (int) 1,
'progress' => null,
'progress_weight_id' => (int) 2,
'priority' => (int) 1,
'status_id' => (int) 2,
'shared_folder_path' => 'path_to_logo_files',
'created_by' => null,
'created_date' => null,
'price' => (float) 0,
'cost' => (float) 0,
'project_status' => object(App\Model\Entity\ProjectStatus) {
'id' => (int) 2,
'status' => 'Running',
'[new]' => false,
'[accessible]' => [
'status' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'ProjectStatus'
},
'[new]' => false,
'[accessible]' => [
'name' => true,
'task_type_id' => true,
'role_id' => true,
'instructions' => true,
'date_start' => true,
'date_end' => true,
'total_minutes' => true,
'assigned_to' => true,
'assigned_from' => true,
'customer_id' => true,
'progress' => true,
'progress_weight_id' => true,
'priority' => true,
'status_id' => true,
'shared_folder_path' => true,
'created_by' => true,
'created_date' => true,
'price' => true,
'cost' => true,
'task_type' => true,
'role' => true,
'users_to' => true,
'users_from' => true,
'users_by' => true,
'user' => true,
'customer' => true,
'progress_weight_label' => true,
'project_status' => true,
'transactions' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'TasksTo'
}
]
【问题讨论】:
-
这是错字吗?
'foreignKey' => 'assigned_From',_F... ?