CakePHP:在链接表中显示名称而不是ID

时间:2019-01-04 11:47:59

标签: php cakephp

我是 CakePHP 和PHP的新开发人员。

  

我有2个表:TasksUsersCustomers

在其他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'

    }
]

1 个答案:

答案 0 :(得分:1)

<td><?php echo $task->assigned_from ?></td>   
//It displays user id, I want to display user name

类似的东西:

<?= $task->users_from->name ?>

<td><?php echo $task->customer_id ?></td>
//Display Customer name instead of Customer_id

喜欢:

<?= $task->customers->name ?>