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 |
[/xml]
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;
$forumImage->save(false);
}
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…
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
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 …
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,
<?php
$userGallery = new UserGallery();
foreach ($userGallery->forumPosts as $photoimage) {
echo CHtml::image(Yii::app()->request->baseUrl.'/images/post/'.$photoimage->forum_image,'',
array('style'=>'width:300px;height:250px;'));
}
?>
// $forum is a forum object
// find the image using forum's image_id
$image=Gallery::model()->findByPk($forum->photo_id);
echo CHtml::image('images/'.$image->name);
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 );
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
<?php
foreach ($data->photo as $image) {
echo CHtml::image(Yii::app()->request->baseUrl.'/images/post/'.$image->forum_image,'',array('style'=>'width:300px;height:250px;'));
}
?>
Error:
PHP notice
Trying to get property of non-object
please help me to get userGallery forum_image field to forum_post page?
// in action
public function actionView($id)
{
// here loadModel() is already available when you generate CRUD using gii
$model = $this->loadModel($id);
$this->render('view',array('model'=>$model));
}
// 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