confused on retrieving fk value.

I read a bunch, still confused (more than usual).

I want to display a fk value in a simple view file. This is confusing the **** out of me.


Projects can have many comments. Comments are entered by one person.

model - comments:

public function relations()


		'deliverable' => array(self::BELONGS_TO, 'Deliverable', 'deliverable_id'),

		'issue' => array(self::BELONGS_TO, 'Issue', 'issue_id'),

		'person' => array(self::BELONGS_TO, 'Person', 'person_id'), <--------- 

How can i retrieve the fk value from person in a view file. I have tried many different solutions, and still getting errors.

I can do it in a grid, list, etc… But can’t seem to understand why i can’t just do it in a loop.

Here is my last attempt in my view file.

		<?php echo (CHtml::encode($comment->person->fullName)); ?>


		<?php echo date('F j, Y \, h:i a',strtotime($comment->date_created)); ?>


returns an error. BTW - person is the relation and fullName is a function within the person model.

Please explain or point me in the right direction.

Signed improving, but still going nuts.

This looks pretty much right:

			<?php echo (CHtml::encode($comment->person->fullName)); ?>

But it will barf if you have a comment that has no person connected to it.

			<?php echo (CHtml::value($comment, 'person.fullName')); ?>

Will solve this.

Is fullName an attribute or a function in the person model?

If it’s an attribute then you can access it as you do above, $person->fullName. If it’s a function you need to rename it to getFullName() to make it a “getter” function. You can then use $person->fullName to call the function.

Documentation on getters and setters:

I don’t understand something.

I use this:

<?php echo (CHtml::value($comment, 'person.fullName')); ?>

It works.

I use this:

 <?php echo (CHtml::encode($comment->person->fullName)); ?>

returns an non-object error

In the db, all comments must be entered by a person (person_id not null). All my records have a valid person_id. So i don’t understand.

It may be something in my code… i am not sure… will have to dig deep.

Thank you for solving this. Many thanks.