0

I might be missing the point on this, but I am trying to echo out a HABTM value in my index, and I cannot seem to get the data.

For example, I can echo these relationships with no issue:

<?php echo $plan['Age']['name']; ?> <br />

<?php echo $plan['Applicant']['name']; ?> <br />

As you can see from the _id recursive relation on the Plan model.

Hope my question is clear. Just not sure what to do on this. Can't seem to resolve it no matter the combination of vars I try.

tereško
  • 58,060
  • 25
  • 98
  • 150
OldWest
  • 2,355
  • 7
  • 41
  • 61
  • Are you talking about a field that is *in* your HABTM table? Or, are the above relationships not HABTM? – Stephen Jan 27 '11 at 21:11
  • I have tables: plans, zips and plans_zips. I am trying to echo the related zip data in a plan index view. As my example echos above, I am able to echo $plan['Age']['name'] etc.. from the ages table as its related to the plans table. BUT since there is no _id field for zip in the plans (because zip is a HABTM), I am a bit stuck on this. Does this help clarify? – OldWest Jan 27 '11 at 21:22

2 Answers2

1

For HABTM, the array is numerically indexed:

<?php echo $plan['Zip'][0]['value']; ?> <br />
<?php echo $plan['Zip'][1]['value']; ?> <br />
<?php echo $plan['Zip'][2]['value']; ?>

Since you are doing the find call on the Plan model, make sure you are defining the HABTM relationship in the Plan model, though preferably in both models.

er... unless my memory fails me. It might be this structure:

<?php echo $plan['Plan']['Zip'][0]['value']; ?> <br />
<?php echo $plan['Plan']['Zip'][1]['value']; ?> <br />
<?php echo $plan['Plan']['Zip'][2]['value']; ?>

Best bet is to use var_dump($plan) or print_r($plan) and examine the structure of the array.

Stephen
  • 18,827
  • 9
  • 60
  • 98
  • Well. I am able to get it like this: , but that just gives me record 1 of the array list. I am trying to get just the Zip that matches the find. – OldWest Jan 27 '11 at 21:38
  • You never mentioned the State model... how does it play in? – Stephen Jan 27 '11 at 21:41
  • Plan ($plan) has state_id. Each zip belongs to a state. – OldWest Jan 27 '11 at 22:05
  • I am trying this inner loop which brings a result like: Array ( [0] => Array ( [id] => 1 [state_id] => 1 [title] => 97378 ) [1] => Array ( [id] => 2 [state_id] => 1 [title] => 97128 ) – OldWest Jan 27 '11 at 22:07
  • And as you can see, I am trying to snag the 'title' data. – OldWest Jan 27 '11 at 22:08
  • Opps. Here is the inner loop: $i = 0; foreach($plan['State'] as $state): echo $state[$i]; ?> it returns the previous thread data. – OldWest Jan 27 '11 at 22:08
  • So I figured the index 0,1,2 etc.. could access title like this: echo $state[$i]['title']; but I get a fatal error on that: Fatal error: Cannot use string offset as an array in... – OldWest Jan 27 '11 at 22:10
0

Thank you for your assistance : ) I solved by doing this:

foreach($plan['Zip'] as $zip): 
echo $zip['title']; ?>
<?php endforeach; ?>

My recursion runs deep, so I did not realize I call the Zip table direct and run an innde foreach to parse the Zip array.

OldWest
  • 2,355
  • 7
  • 41
  • 61