Wednesday, December 3, 2008

Re: ORM, fetching related rows of corresponding model: undefined index..

It's a typo, and a pretty obvious one.
change $question['Answer'] to $question['Question']['Answer'] and it
should be fine, assuming there are no other typos/errors (haven't
checked).

-J.

On Dec 3, 1:27 pm, Tanay <saita...@gmail.com> wrote:
> i have two dbs questions , and answers..
>
> corresponding models
>
> questions has many answers
> answers belogns to questions
>
> in questions_controller, i have this home function
>  function home() {
>           $this->Question->recursive = 1;
>           $this->set('questions', $this->Question->find('all'));
>        }
>
> in my home.ctp, i tried to show the corresponding answers using the
> code below
> i took a question from an array of questions using foreach
> foreach question, $question['Answer'] is expected to give array of
> answers, but it says  Undefined index:  Answer
> THAT LIne is marked in code below
>
> home.ctp:
>
>     <?php if(empty($questions)): ?>
>        <p class="no_answer">No Questions yet. Be the first one to
>                  post a Question!</p>
>     <?php else: ?>
>        <dl>
>        <?php foreach ($questions as $question): ?>
>           <dt><span><?php e($question['Question']['questioner']);
>            ?></span></dt>
>                      <dd>
>              <?php e($html->link($question['Question']['question']
>     .'?', array('action' => 'show', $question['Question']['id']))); ?>
>              <?php
>                 $answer_count = count($question
> ['Answer']);   //////////  <<-----------------ERROR HERE
>                 if(!$answer_count)
>                    e("(no answers yet)");
>                 else if($answer_count == 1)
>                    e("(1 answer)");
>                 else
>                    e("(".$answer_count." answers)");
>               ?>
>           </dd>
>        <?php endforeach; ?>
>        </dl>
>     <?php endif; ?>
>
> the above code is a part of project in book CakePHP application
> Development,
> the code is same as in book
> is it due to some different version of cakephp that author used?
> i use latest 1.2 version
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: