Cactiverecord Relation Doesn't Work In One Case

Hello,

I am playing around with Yii for a few weeks and it is very helpful, but sometimes I strange things, or how you call it: esoteric things happen.

I started with the Blog Tutorial, so I decided to extend it for better understand how yii works.

My testarea runs under Windows XP / 7 and XAMPP with MySql (InnoDB)

The database has 4 Tables: Blog -> Tag -> Entry -> Comment (Always 1:n)

The generated relation in Blog and Tag work fine:

models/Blog - relations:


'tags' => array(self::HAS_MANY, 'Tag', 'blog_id'),

_view.php:


echo $data->tags[0]->tagtitle;

models/Tag - relations:


'entry' => array(self::HAS_MANY, 'Entry', 'tag_id'),

_view.php:


echo $data->entry[0]->entrytitle;

Now I have problems:

models/Entry - relation:


'comments' => array(self::HAS_MANY, 'Comment', 'entry_id'),

_view.php:


echo $data->comments[0]->commenttitle

Gives the Error:

PHP notice - Undefined offset: 0


41       echo $data->comments[0]->commenttitle;

I have absolutely no idea what the problem is. First I deleted the DB and re-created it.

Then I count the array: 1 (Yes there is data in the table).

The varDump shows me the Information, but I can’t get the data like you seen above!


array

(

    0 => Comment#1

    (

        [CActiveRecord:_md] => CActiveRecordMetaData#2

        (

...




 ...

[CActiveRecord:_attributes] => array

        (

            'id' => '1'

            'commenttext' => 'This is a comment'

            'commentdate' => '2012-12-11 14:09:08'

            'spot_id' => '1'

        )

...



I hope the Information is enough to help me and solve the problem. or do you need the whole varDump?

Thank you for your help!

Dianamo

More info would help, but it sounds like some of the records have no comments associated with them. You could check them more safely like this:




if (count($data->comments))

    echo $data->comments[0]->commenttitle;



Also, I don’t know if it’s a typo, but commenttitle doesn’t seem to appear in your attributes.

Hi Keith,

thank you for your HELPFUL answer. It works with the if.

But I have no idea why -.-

First: No typo, I accidently deleted commenttitle from the attributes array with other attributes

Second: There is one Comment for this Entry, you see in the varDump of the array.

Can you explan to me why it works with the if and without not?

And why it works in my other two view files without if, but not in this one?

Thanks, Dianamo

it’s weird! Can you use ‘foreach’ statement to see if it echos anything, like this:




foreach($data->comments as $comment){

	echo $comment->commentTitle . '<br/>';

}



if it doesn’t then maybe the foreign key in your ‘Comment’ table is NOT ‘entry_id’? maybe a typo or something?

I suspect that only certain records have no comments and that you’re not encountering those on the other pages. In any case, any time you’re trying to access a specific index like this you should guard it in this way.

Damn Keith, that was the problem.

One Entry hasn’t a comment and that was the failure. I only checked the first entry for data, not the ohters…

All other records had data for relations, except one damn entry -.-

Thanks a lot!