1

我的目标是在视图中显示相关子模型(一旦删除)的记录。我知道将递归值设置为“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'] )

谢谢您的帮助。保罗

4

1 回答 1

2

您可以使用Containable Behavior 详细指定要从关联模型、关联模型到关联模型等获取哪些字段。然后你会发现数组中的所有数据$tournament,就像链接中的例子一样。

编辑(响应 OP 评论)。我认为问题可能出在使用魔术findBy方法上。您需要用标准替换它find,就像在这个例子中一样

$tournament = $this->Tournament->find('first', array(
    'conditions' => array(
        'slug' => $slug
    ),
    'contain' => array(//Put here the settings for Containable
    )
));
于 2011-02-25T16:42:35.120 回答