Yii Call Beforesave() In Other Model's To Be Save

I have created two tables as, forum_post and gallery.[xml]forum_post table:

|id | user_id | ststus | photo_id |

|1 | 1 | hi…! | NULL |

|2 | 1 | hello! | NULL |

|3 | 1 | NULL | 1 |

|4 | 1 | NULL | 2 |

user_gallery table:

|id | user_id | image | video |

|1 | 1 | 1.jpg | NULL |

|2 | 1 | new.gif| NULL |


When, user upload the image file in the user_gallery table, i want to create one row in the forum_post table and store the gallery id into the forum_post-> image field. as well as the user id also stored in the forum_post table.

My model code in the ForumPost is:

public static function addForumImage($image='')	{

		// If the Institution is not already added in the Institution table, it will be added automatically.

		$forumImage = ForumPost::model()->find('LOWER(photo_id) = ?',  array( strtolower($image)));

		if (!$forumImage) {

			$forumImage = new ForumPost;

			$forumImage->photo_id = $image;



		return true;


UserGallery beforeSave function is:

protected function beforeSave() {

		if (parent::beforeSave()) {

	ForumPost::addForumImage($this->id, $this->user_id);

			return true;


		return false;


My table relationship is, user_gallery->image refers the forum_post->photo_id.

Now, the image is stored in the user_gallery folder and i dint get the id in the ForumPost model… Please any one help me… :(

Just change "return true" to "return $forumImage->id"

Now i’m getting the following error message:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`nichehands`.`forum_post`, CONSTRAINT `fk_forum_post_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE). The SQL statement executed was: INSERT INTO `forum_post` (`status`, `photo_id`, `updated`, `created`) VALUES (:yp0, :yp1, :yp2, :yp3)

hello selvakumar in forumpost controller create object for forumpost model and if the user uploaded any image then create another object for gallery and save the gallery prior to forumpost and then set forumpost photo id as


$forumpost->photo_id = $gallery->id;


Seems like you have no idea of what you’re trying to do :)

First of all, your function addForumImage takes one param, and you’re passing two.

I suppose the signature should look like this

addForumImage($image_id, $user_id)

and no default values.

Also don’t forget to add $forumImage->user_id = $user_id;

Next, I don’t understand for what purpose do you need “the id in the ForumPost model”

PS. Your error message tells you that you have tried to set user_id to non-existing value.

When i’m using this method the will bw

( ! ) Fatal error: Call to undefined method stdClass::save() in C:\wamp\www\nh\protected\controllers\UserGalleryController.php on line 366

The user_gallery id is stored in the Forum_post photo_id filed. its because of i want to display only the forum_post view in the index page. so i have created the relationship as like this.

$forumPost->photo_id is refers to the $user_gallry->id …

read the yii guide collecting tabular input. it will help you.

Thanks Ahamed Rifaideen,

i have stored the value in both the table, now how can i fetch the value from another table. example, i want to display the image in the forum view, but in the forum i have only stored the photo id. . .

I have tried like this,


	$userGallery = new UserGallery();

	foreach ($userGallery->forumPosts as $photoimage) {

	    echo CHtml::image(Yii::app()->request->baseUrl.'/images/post/'.$photoimage->forum_image,'',




but the error is

Trying to get property of non-object


How to display the image in the forum_post view? :(

you can find the image like this

// $forum is a forum object

// find the image using forum's image_id


echo CHtml::image('images/'.$image->name);

PHP notice

Trying to get property of non-object 

My _view.php file is,


    $forum = new ForumPost;


    echo CHtml::image(Yii::app()->request->baseUrl.'/images/post/'.$image->forum_image);


hello selvakumar you are just creating object for forumpost only not finding any forumpost so it does not work.

// here 1 is the id of forumpost model replace 1 to your id

$forum = ForumPost::model()->find(1);

$image = UserGallery:: model()-> findByPk( $forum -> photo_id );

echo CHtml:: image (Yii :: app()-> request-> baseUrl. '/images/post/' .$image -> forum_image );

ForumPost model:

public function relations()


	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'photo' => array(self::BELONGS_TO, 'UserGallery', 'photo_id'),

		'user' => array(self::BELONGS_TO, 'User', 'user_id'),

		'video' => array(self::BELONGS_TO, 'UserGallery', 'video_id'),

		'forumPostComments' => array(self::HAS_MANY, 'ForumPostComment', 'post_id'),



userGallery model:

public function relations()


	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'forumPosts' => array(self::HAS_MANY, 'ForumPost', 'photo_id'),

		'forumPosts1' => array(self::HAS_MANY, 'ForumPost', 'video_id'),

		'user' => array(self::BELONGS_TO, 'User', 'user_id'),



forum_post page _view.php


	foreach ($data->photo as $image) {

	echo CHtml::image(Yii::app()->request->baseUrl.'/images/post/'.$image->forum_image,'',array('style'=>'width:300px;height:250px;'));




PHP notice

Trying to get property of non-object 

please help me to get userGallery forum_image field to forum_post page?

why are you loop through the

$data -> photo

since it is a belongs_to relation in forumpost model. so just print the image directly like this

echo 'photo id='.$data->photo->id

$id = isset ($_GET[‘id’]) ? ($_GET[‘id’]):null;

$forum = ForumPost::model()->findByPk($id);

$image = UserGallery:: model()-> findAll();


In that code using the line

$image = UserGallery:: model()-> findAll();

It display the arrays of values.

If i give the line instead of above line,

$image = UserGallery:: model()-> findByPk($id);

It throw the null value

now what i can do plz help me :(

// in action

public function actionView($id)


// here loadModel() is already available when you generate CRUD using gii

 $model = $this->loadModel($id);



// in view.php

echo 'photo id='.$model->photo_id.'<br>';

// here $model->photo is the relation defined in model

echo "<img src='".$model->photo->image ."' />";

// similar codes here