【问题标题】:Cakephp 3.6: Multiple foreign keys between 2 tablesCakephp 3.6:两个表之间的多个外键
【发布时间】:2018-12-19 22:08:14
【问题描述】:

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

我有 2 张桌子:TasksUsers

其中Tasks 将这些外键链接到Users 表:Assigned_fromAssigned_toCreated_by

所以我在UsersTable.php 中插入了这些行:


函数初始化:

$this->hasMany('TasksTo', [
            'foreignKey' => 'assigned_to',
            'className' => 'Tasks'
        ]);
$this->hasMany('TasksFrom', [
            'foreignKey' => 'assigned_From',
            'className' => 'Tasks'
        ]);
$this->hasMany('TasksBy', [
            'foreignKey' => 'created_by',
            'className' => 'Tasks'
        ]);

函数构建规则:

$rules->add($rules->existsIn(['assigned_to'], 'TasksTo'));
$rules->add($rules->existsIn(['assigned_from'], 'TasksFrom'));
$rules->add($rules->existsIn(['created_by'], 'TasksBy'));

相应地在 TasksTable.php 中: 函数初始化:

$this->belongsTo('UsersTo', [
        'foreignKey' => 'assigned_to',
        'className' => 'Users'
    ]);
$this->belongsTo('UsersFrom', [
        'foreignKey' => 'assigned_from',
        'className' => 'Users'
    ]);
$this->belongsTo('UsersBy', [
        'foreignKey' => 'created_by',
        'className' => 'Users'
    ]);

函数构建规则:

$rules->add($rules->existsIn(['assigned_to'], 'UsersTo'));
$rules->add($rules->existsIn(['created_by'], 'UsersBy'));
$rules->add($rules->existsIn(['assigned_from'], 'UsersFrom'));

所以为了在用户视图中显示这些信息,我在 UsersController.php 中添加了这些行:

public function view($id = null)
{
    $user = $this->Users->get($id,[
        'contain' => ['TasksTo', 'TasksFrom', 'TasksBy']
    ]); 
    $this->set('user', $user);
}

如何修改view.ctp 以显示所有这些信息?

这是当前代码:

<div class="related">
    <h4><?= __('Related Tasks') ?></h4>
    <?php if (!empty($user->tasks)): ?>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th scope="col"><?= __('Priority') ?></th>
            <th scope="col"><?= __('Name') ?></th>
            <th scope="col"><?= __('Instructions') ?></th>
            <th scope="col"><?= __('Date_start') ?></th>
            <th scope="col"><?= __('Date_end') ?></th>
            <th scope="col"><?= __('Total_minutes') ?></th>
            <th scope="col"><?= __('Assigned_from') ?></th>
            <th scope="col"><?= __('Customer_id') ?></th>
            <th scope="col"><?= __('Progress') ?></th>
            <th scope="col"><?= __('Shared_folder_path') ?></th>
            <th scope="col" class="actions"><?= __('Actions') ?></th>
        </tr>
        <?php foreach ($user->tasks as $tasks): ?>
        <tr>
            <td><?= h($tasks->priority) ?></td>
            <td><?= h($tasks->name) ?></td>
            <td><?= h($tasks->instructions) ?></td>
            <td><?= h($tasks->date_start) ?></td>
            <td><?= h($tasks->date_end) ?></td>
            <td><?= h($tasks->total_minutes) ?></td>
            <td><?= h($tasks->assigned_from) ?></td>
            <td><?= h($tasks->customer_id) ?></td>
            <td><?= h($tasks->progress) ?></td>
            <td><?= h($tasks->shared_folder_path) ?></td>
            <td class="actions">
                <?= $this->Html->link(__('View'), ['controller' => 'Tasks', 'action' => 'view', $tasks->id]) ?>
                <?= $this->Html->link(__('Edit'), ['controller' => 'Tasks', 'action' => 'edit', $tasks->id]) ?>
                <?= $this->Form->postLink(__('Delete'), ['controller' => 'Tasks', 'action' => 'delete', $tasks->id], ['confirm' => __('Are you sure you want to delete # {0}?', $tasks->id)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>
</div>

【问题讨论】:

    标签: php cakephp cakephp-3.x


    【解决方案1】:

    您的hasMany 关联将作为关联名称的小写和下划线版本提供:

    $user->tasks_to   //TaskTo association
    $user->tasks_from //TasksFrom association
    $user->tasks_by   //TasksBy association
    

    因此,要在视图中显示这些数据,您需要分别循环这些关联,例如:

    <?php foreach($user->tasks_to as $task): ?>
        <td><?= h($task->priority) ?></td>
        <td><?= h($task->name) ?></td>
        <td><?= h($task->instructions) ?></td>
        <td><?= h($task->date_start) ?></td>
        <td><?= h($task->date_end) ?></td>
        <!-- and so on... -->
    <?php endforeach; ?>
    

    更多信息可以在 CakePHP 文档中找到:

    CakePHP 3 Conventions

    CakePHP 3 Associations

    【讨论】:

    • 谢谢!但是我有两个问题如果$user-&gt;tasks_to 为空会发生什么?那我什么都不会显示对吧?即使其他字段中有值?以及如何在 $task->assigned_from、$task->assigned_to 等中显示用户名而不是 id?
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多