2

我的目标是在视图中显示相关子模型(一旦删除)的记录。我的问题是当我尝试输出以下查询时收到“通知 (8):未定义索引:”错误消息。

锦标赛有很多锦标赛详情,锦标赛详情有很多更新。我正在尝试显示锦标赛视图上显示的每个锦标赛详细信息的所有更新。

所以我的问题是如何正确解决错误消息并在锦标赛视图页面上显示每个锦标赛详细信息的更新。

我在“比赛”控制器视图操作中的查找数据是:

$updates = $this->Tournament->TournamentDetail->Update->find('all', array( 'tournament_details.tournament_id'=> $this->data['Tournament']['id']));

在视图中,我有这个代码。

<?php foreach ($tournament['Update'] as $update): ?>
 <h3>Updates</h3>
 <h1><?php echo $update ['title']; ?></h1>
 <p><?php echo $update ['body']; ?></p>
 <hr/>
<?php endforeach; ?>

这与我用于其他相关子记录的“foreach”代码相同,没有问题。我确实使用 debug() 函数进行了调查,但没有看到我遗漏了什么。

调试函数的 $updates 数组的输出如下所示:

Array
(
    [0] => Array
        (
            [Update] => Array
            (
                [id] => 1
                [title] => first round matches start today
                [date] => 2010-03-19
                [body] => this tournament's first round matches start today.
                [tournament_detail_id] => 4
                [homeStatus] => no
            )

有没有一种特殊的方法可以显示这么深的记录?

与往常一样,提前致谢。保罗


更新:2011 年 2 月 23 日 经过一些测试,我似乎遇到的问题是找到正确的值并将其传递给 $updates 变量;

总而言之,我希望 $updates 变量保存当前的“tournament_details_id”。这将在“锦标赛”视图中显示相关的更新记录。

我仍然是初学者,很可能忽略了显而易见的事情。

以下是型号信息:

class Tournament extends AppModel {
 var $name = 'Tournament';
 var $hasMany = array(
    'TournamentDetail' => array(
    'className' => 'TournamentDetail',
'foreignKey' => 'tournament_id',)


class TournamentDetail extends AppModel {
var $name = 'TournamentDetail';
var $belongsTo = array(
    'Tournament' => array(
        'className' => 'Tournament',
        'foreignKey' => 'tournament_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

class Update extends AppModel {

var $name = 'Update';
var $belongsTo = array(
         'TournamentDetails' => array(
        'className' => 'TournamentDetails',
        'foreignKey' => 'tournament_detail_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

控制器数据:

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( 'tournament_details_id' => $this->data['TournamentDetails']['id'] )));

$this->set(compact('tournament','updates', $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; ?>

我已经通过将锦标赛详细信息“id”作为整数添加来对此进行了测试,它会提取正确的“更新”记录。但是,我似乎在配置查找操作以执行相同操作时遇到问题。

一如既往,我感谢您的帮助。

谢谢保罗

4

2 回答 2

1

Based on your debug output, it seems that your $tournaments variable consists of an array (instead of the associative array) so you might want to use the foreach to access each of those elements:

foreach ($tournaments as $update):
?>
    <h3>Updates</h3>
    <h1><?php echo $update ['Update']['title']; ?></h1>
    <p><?php echo $update ['Update']['body']; ?></p>
    <hr/>
<?php endforeach; ?>
于 2011-02-21T04:38:03.947 回答
0

看来您使用了错误的变量,您谈论了该$updates变量,但您$tournament在视图中使用了该变量。

<?php foreach ($updates['Update'] as $update): ?>
 <h3>Updates</h3>
 <h1><?php echo $update ['title']; ?></h1>
 <p><?php echo $update ['body']; ?></p>
 <hr/>
<?php endforeach; ?>

此外,您还没有发布整个控制器方法,其中包括该 find 但您可能还没有将数据发送到视图

$this->set(compact('updates'));
于 2011-02-21T04:03:55.380 回答