Please Help Me Understand What I'm Doing Wrong with Relations and Pivot Table

I’m very new to Yii. I’m using the current stable version of Yii.

I’ve read a lot on the use of relations and pivot tables, but I still cannot get this to work. I think I’m missing something, but I cannot find what it is.

I have a table named Biography with an ID and some columns with data about a person. I have a table named Media which includes an ID, fileName and fileLocation (URL). I have a pivot table named BioMedia with an ID, profileId (the Biography ID) and mediaId (the media table ID). I have generated models for all 3 tables.

In the Biography model, I have:


public function relations()

	{

		return array(

			'media'=>array(self::MANY_MANY, 'Media',

                'BioMedia(profileId, mediaId)'),

		);

	}

In the Media table, I have:


public function relations()

	{

		return array(

			'Biography'=>array(self::MANY_MANY, 'Biography',

            'BioMedia(mediaId, profileId)'),

		);

	}

In my view, I have:


<span style="float:left;">

            	<?php $images = Biography::model()->with('media')->findAll();

				if ($images == !NULL) {

					foreach ($images as $image):

				 	?>

                	<img src="<?php echo Yii::app()->request->baseUrl; echo $image; ?>" alt="biography images" />

                	<?php endforeach; 

		} ?>

           	</span>

I get the error "Object of class Biography could not be converted to string" when I run this. If I take out the foreach, I get just get Array.

What am I doing incorrectly?

Thanks for any help you can provide.

I think it should be :


<span style="float:left;">

                <?php $biographies = Biography::model()->with('media')->findAll();

                                if ($images == !NULL) {

                                        foreach ($biographies as $biography):

                                        ?>

                        <img src="<?php echo Yii::app()->request->baseUrl; echo $biography->image; ?>" alt="biography images" />

                        <?php endforeach; 

                } ?>

                </span>

Not sure, because haven’t looked at your Biography table structure yet.

Replace


    if ($images == !NULL) {



With


    if (!empty($images)) {



Then check.!!

And one more thing…after reviewing your code…it seems that you are not following a MVC standards.It is not a good approach a programming.You are running Find queries on view file.

Thanks for the help. It’s still not working though. @Junxiong, that won’t work. $images was a variable I defined in that script you changed to $biography, so now it’s undefined. Therefore, it also can’t work in the second instance you used $biography->image. There is no column named image. I am trying to pull the rows for the column “fileLocation” from the Media table that correspond to the ID column in the Biography table (via the pivot table). I tried changing it to $biography->fileLocation and still got the error: Property “Biography.fileLocation” is not defined.

@jayant, thanks for letting me know about that. I’ll educate myself and correct it. I was following the layout I saw on one of the Yii documentation pages.

Any other ideas?

Seems impossible to me… I do defined $biography. It is located at the foreach. I just rename the $images because I thought the name doesn’t represent the real value. My bad. But since you want to access other attribute, then it is better we stick to your code then.

If you want to access the other tables attribute, it seems like you need one more foreach to access it. One foreach for Biographies findAll() and one is for the array of Media


<span style="float:left;">

<?php $images = Biography::model()->with('media')->findAll();

		if (!empty($images)) {

			foreach ($images as $image):?>

				<?php foreach($image as $media): //add one more foreach?>

					<img src="<?php echo Yii::app()->request->baseUrl; echo $media->fileLocation;//use $media instead $images ?>" alt="biography images" />

				<?php endforeach; //add the endforeach  ?>

	<?php endforeach; 

} ?>

</span>