我是 CakePHP 和PHP的新开发人员。
我有2个表:
Tasks
和Users
。
在其他Tasks
中,这些外键链接到Users
表:Assigned_from
,Assigned_to
,Created_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>
答案 0 :(得分: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文档中找到: