Я новый разработчик в 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'
]);
Функция buildrules:
$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>