Cakephp 3.6:两个表之间的多个外键

Cakephp 3.6: Multiple foreign keys between 2 tables

我是 CakePHP 和 PHP.

的新开发者

I have 2 tables: Tasks and Users.

除其他外,Tasks 将这些外键链接到 Users table: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>

您的 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