我的目标是在视图中显示相关子模型(一旦删除)的记录。我知道将递归值设置为“1”会返回当前模型、其所有者以及它们的关联模型。
我不知道该怎么做是在视图中显示它。
我的方法是在控制器中创建一个变量来保存相关模型 ID 的值,但这不起作用。
*我还应该提到,我在比赛中使用了 sluggable 行为,所以使用的不是 $id 而是 $slug。
以下是模型视图控制器的详细信息:
模型
锦标赛 - 有很多锦标赛详情
比赛详情 - 有很多更新
更新 - 属于锦标赛详情
锦标赛控制器动作 = 查看
class TournamentsController extends AppController
function view($slug = null) {
if (!$slug) {
$this->Session->setFlash(__('Invalid Tournament.', true));
$this->redirect(array('action'=>'index'));
}
$this->Tournament->recursive = 1;
$tournament = $this->Tournament->findBySlug($slug);
$updates = $this->Tournament->TournamentDetail->Update->find('all', array('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )) );
$this->set(compact('tournament','updates' ));
比赛视图。这是显示“比赛详细信息”和(理想情况下)相关的比赛详细信息“更新”
<h2><?php echo $tournament['Tournament']['name']; ?></h2>
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<h1><?php echo $tournamentDetail ['title']; ?></h1>
<p><?php echo $tournamentDetail ['body']; ?></p>
<?php endforeach; ?>
<?php foreach ($updates['Update'] as $update): ?>
<h4>Update: <?php echo $update ['date']; ?></h4>
<p> <?php echo $update ['Update'] ['title']; ?></p>
<p><?php echo $update ['Update'] ['body']; ?></p>
<?php endforeach; ?>
当我调试 $updates 时,这就是我所看到的:
Array
(
)
我不知道我在这里做错了什么。
使用以下方法构造查找条件是否不正确:
('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )
谢谢您的帮助。保罗