1

I'm using containable to pull associated records into the view action and am getting an error message when looping through results.

Also, I'm using a 'sluggable' behaviour, so the find operation has a condition to search by this variable.

When I debug the find variable in the view, I do see the correct records. But when I try and loop through them in the view I get the 'Notice (8): Undefined index: error.' Ideally I liked to understand how to trouble shoot this error since it happens occasionally.

The model setup is:

tournaments have many tournamentDetails

tournamentDetails have many updates

The records I'm trying to display are:

tournament->tournamentDetails->updates

The tournament controller looks like this:

    $tournament = $this->Tournament->find('first', array(
         'conditions' => array( 'slug' => $slug),
         'contain' => array(
        'TournamentDetail' => array(
             'Update' => array('order' => 'Update.id DESC'),
        ))));

The tournament view action looks like this:

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

The 'update' data in the view when in debug looks like:

[Update] => Array
                    (
                        [0] => Array
                            (
                                [id] => 2
                                [title] => Indoor Challenge
                                [date] => 2010-03-19
                                [body] => Congratulations 2010 Champion 
                                [tournament_detail_id] => 4
                                [homeStatus] => no
                            )

                        [1] => 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 something really obvious that I'm overlooking when looping through the 'updates' ?

Thanks, Paul

Resolution

Thanks everyone for the input. After taking a few steps back it occured to me what was happening with the 'undefined index' and comments about how to loop.

The problem was that the foreach loop wasn't nested, it only looped on the first level of association. The forearch loop deal with 'tournaments' which have many 'tournamentDetails'.

The loop needed to go one level deeper to 'tournamentDetails that have many 'updates'.

Here's the code that resolved this in the view.

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

If others are looking to understand how to use the containable behavior with more than one level of association, just remember that you may have to have nested foreach loops in the view to display the results you're after.

Cheers, Paul

Nunser
  • 4,512
  • 8
  • 25
  • 37
Paul
  • 237
  • 5
  • 24
  • can u please specify which index is missing?? – RSK Feb 28 '11 at 18:51
  • the index that's missing is 'update', it's the foreach loop above. The error message states 'Notice (8): Undefined index: Update', but it has been defined in the containable settings, no? – Paul Mar 01 '11 at 00:26
  • i am terribly sorry Paul, please check edit 0 again, as i modified it. could save you some work. – benjamin Mar 03 '11 at 01:08
  • Hi everyone, thanks very much for the help. I have resolved the issue, took Benjamin's advice and stepped back. I've posted the answer below. – Paul Mar 04 '11 at 00:41

5 Answers5

1

Paul,

given that the other things are right, could it be that you are mixing singular, plural, uppercase and lowercase? In particular the update/updates in various combinations.

Edit 0: Quick shot?: There simply is no such index defined, notice that you are working with an array consisting of arrays. It seems you have to use another loop nested in the first.

Edit 1: Unfortunately, I can only provide quite generic help, as I do not sit infront of your code. From your post and comments, I would guess that some variable names got mixed up. Just going over the whole dataflow normally does it. What is the exact variable name in the controller which delivers the correct data? If there is one, is it properly set from the controller to the view? Does the view address it correctly.

You are thinking great, this is what I did the last week, but I am quite sure that should do it. Clean out all the view code which currently makes use of any variables from the controller, proceed as described above keeping the dataflow in mind (but do not spend more than one hour on this).

If it does not work:

Something is foobared. Before you spend another week in despair, I would

  1. try it without the containable behavior, see if it works
  2. try to set up exactly the
    same scenario in a completely new
    CakePHP environment (in your case 1.2.5), see if it works

If you achieve your goals, try to see what went wrong in the original (often a face slapping moment).

If not:

  1. try to see if there is a known bug
  2. consider upgrading (or try to achieve your goals in 1.3.7 first)

Good luck, Benjamin

benjamin
  • 2,185
  • 1
  • 14
  • 19
  • Benjamin, I hear you. I've been looking at this for days, and have tried many variations. Still I get the 'undefined index' error. What's so strange about it is that when I debug in either the controller or view, the output is exactly what I'd expect. Which makes me think the index is defined and correct, since it outputs what I want. But when debug is off I'm back to getting errors in the view. When I try creating a variable 'updates' with a find operation looking for 'tournamentDetails.id = 4' and then loop through that in the view it works without errors. So what's teh difference? Any ideas – Paul Mar 02 '11 at 15:47
  • Thanks again Benjamin, I'll take your advice and work through as you've described. I'll let you know the outcome. Cheers, Paul. – Paul Mar 03 '11 at 01:06
0

I think i understand you problem and please Try looping in this way

<?php foreach ($tournament ['Update'] as $update): ?>
   <h3>Update: <?php echo $update['date']; ?></h3>
     <h4><?php echo $update['title']; ?></h4>
     <p><?php echo $update['body']; ?></p>
   <hr/>
<?php endforeach; ?>
RSK
  • 17,210
  • 13
  • 54
  • 74
  • Thanks for the suggestion. I had tried this before but did try it again. It still outputs the same 'undefined index: Update' error message. Any other suggestions? Thanks, Paul – Paul Mar 01 '11 at 03:16
0

RSK's answer should be right, so since it's still not working you're probably not giving all the information needed. What does this output: debug($tournament); ?

vindia
  • 1,678
  • 10
  • 14
  • Hi Vindia, as mentioned above I've spent days on this error without luck. I also mentioned that when I pass in an integer to the find operation mentioned above I don't get an error. Don't know what the difference is. Also when I debug $tournament I get exactly what's in the last bit of code showing [Update] in debug. Could it be the version of cake I'm using? The version is 1.2.5, thanks for your help. – Paul Mar 02 '11 at 15:51
  • I doubt that it is the cake version, just give my Edit0 a try. – benjamin Mar 03 '11 at 00:49
  • Where exactly are you entering an integer in the find operation? You mean when $slug is an integer, everything works fine? In that case, do some `debug()` prints on the values of `$tournament` and `$update` to see what is different in your output from when `$slug` is a string. – vindia Mar 03 '11 at 10:48
  • Also, try benjamins suggestions: take one step back, dumb down your code and add functionality until it breaks. Use `debug()` a lot in this process and note the differences in the values of your variables. – vindia Mar 03 '11 at 10:49
0

This all assumes your debug information is debug( $tournament ) and not debug( $update )

/* your old code */
<?php foreach ($tournament ['Update'] as $update): ?>
    <h3>Update: <?php echo $update ['Update']['date']; ?></h3>
    <h4><?php echo $update['Update']['title']; ?></h4>
    <p><?php echo $update  ['Update']['body']; ?></p>
    <hr/>
<?php endforeach; ?>

If the debug value you provided was a debug of the $tournament variable then the first part of your loop simply assigns the numerically keyed arrays inside of the tournaments variable to the $update value.

So, when you send $tournament[ 'Update' ] through the loop you are getting a structure in the $update array like the following.

array(
    [id] => 2
    [title] => Indoor Challenge
    [date] => 2010-03-19
    [body] => Congratulations 2010 Champion 
    [tournament_detail_id] => 4
    [homeStatus] => no
)

But in your loop you are trying to access the keyed values as if they exist under an additional layer keyed with 'Update'. That key does not exist in your interior array.

So if my assumption is right - your loop should look like:

/* your old code with edits removing the additional interior Update key */
<?php foreach ($tournament ['Update'] as $update): ?>
    <h3>Update: <?php echo $update['date']; ?></h3>
    <h4><?php echo $update['title']; ?></h4>
    <p><?php echo $update['body']; ?></p>
    <hr/>
<?php endforeach; ?>

There also seemed to be some spacing in your brackets in the original code - I don't know if this is an issue or not but it looked odd to me.

Abba Bryant
  • 4,012
  • 22
  • 18
0

Resolution

Thanks everyone for the input. After taking a few steps back it occurred to me what was happening with the 'undefined index' and comments about how to loop.

The problem was that the foreach loop wasn't nested, it only looped on the first level of association. The forearch loop deal with 'tournaments' which have many 'tournamentDetails'.

The loop needed to go one level deeper to 'tournamentDetails that have many 'updates'.

Here's the code that resolved this in the view.

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

If others are looking to understand how to use the containable behavior with more than one level of association, just remember that you may have to have nested foreach loops in the view to display the results you're after.

Cheers, Paul

Paul
  • 237
  • 5
  • 24