【问题标题】:CakePHP: Display name instead of id in linked tablesCakePHP:在链接表中显示名称而不是 id
【发布时间】:2019-01-07 22:24:44
【问题描述】:

我是 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'

    }
]

【问题讨论】:

  • 这是错字吗? 'foreignKey' =&gt; 'assigned_From',_F... ?

标签: php cakephp


【解决方案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 ?>

【讨论】:

  • 它不工作,我得到错误'试图获取非对象的属性'名称'
  • 什么显示调试($task)?
  • 你需要像这个'project_status'这样创建更深层次的关联
  • 我怎样才能做到这一点?在tasktable.php中我有$this-&gt;belongsTo('Customers', [ 'foreignKey' =&gt; 'customer_id' ]);$this-&gt;belongsTo('ProjectStatus', [ 'foreignKey' =&gt; 'status_id' ]);我看不出它们之间的任何其他区别
  • 示例:'contain' =&gt; ['TasksTo', 'TasksTo.Customers']
猜你喜欢
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多