开发者

cakephp: how to render related models in the view

My objective is to display records for a related child model (once removed) in the view. My problem is that I receive a 'Notice (8): Undefined index:' error message when I try to output the following query.

Tournaments have many tournamentDetails, and tournamentDetails have many updates. I'm trying to display all the updates for each tournamentDetail that is displayed on the tournaments view.

So my question is how to properly resolve the error message and display the updates for each tournamentDetail on the tournaments view page.

My find data in the 'tournaments' controller view action is:

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

In the view, I have this code.

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

This is the same 'foreach' code the I use for the other related child records without problem. I did use the debug() function to investigate but didn't see what I was missing.

The output of the $updates array from the debug function looks like this:

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
            )

Is there a special way to display records this deep?

As always, thanks in advance. Paul


Update: Feb 23/11 After some testing the problem I seem to have is finding and passing the correct value to the $updates variable;

To summarize, I want the $updates variable to hold the current 'tournament_details_id'. This will then display the related update records in the 'tournaments' view.

I'm still a beginner and most likely overlooked the obvious.

Here is 开发者_JAVA百科the model info:

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' => ''
    )
);

Controller data:

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 ));

Tournament view. This is display the 'tournament details' and (ideally) related tournament detail '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; ?>

I've tested this by adding in the tournament details 'id' as an integer and it pulls up the correct 'update' record. However I seem to have problems configuring the find operation to do the same.

As always I appreciate the help.

Thanks Paul


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; ?>


It appears that you be using the wrong variable, you talk about the $updates variable but you're using the $tournament variable in your view.

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

Additionally you haven't posted the whole controller method, which includes that find but you might not have sent the data to the view yet either

$this->set(compact('updates'));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜