Get Specific Values From Another Model(Database_Table)


I want current user id’s(Yii::app()->user->id) specific filed from another model. This is my MySQL query [sql]SELECT id FROM user_gallery where user_id=24 AND uploaded_picture NOT IN(‘NULL’);


And now how to apply this coding in the yii - CDbCriteria… Please help me.

$criteria = new CDbCriteria();


            $criteria->condition="user_id = :user_id AND upload_picture NOT IN('NULL')";

            $criteria->params = array(

                ':user_id' => Yii::app()->user->id



            $model = Gallery::model()->find($criteria);

i did not checked so check this

thanks Ahamed Rifaideen. it works fine. now i want to pass that id to the text field. When I’m printing echo $model;

[b]Recoverable error[/b]

Object of class UserGallery could not be converted to string 

getting this error. How to pass the id to the text field?

you are trying to print the object if you want to print id use this

echo $model->id;

[color="#556B2F"]/* Moved from "Indonesian" to "General Discussion for Yii 1.1.x" */[/color]

it works fine. if echo $model->id; is null, then how can I update the FOREIGN KEY filed. If

echo $model->id;

is present, I want to store ‘id’ otherwise it must be a ‘NULL’ value field.

I have tried check condition if it’s Null passing as null value

$imageFileId = empty($userGallery->id)?  'NULL':$userGallery->id;

is any-other way to do it?

actually what you want to do could you please explain

forum_post table:

When your upload image or content in the forum i want to display the particular user profile picture with the post(as like facebook).

And my table is as follows:



|id | user_id | ststus | photo_id | pro_pic_id |

|1 | 1 | hello! | NULL | 1 |

|2 | 1 | NULL | 2 | 1 |

|3 | 1 | NULL | 3 | 1 |




|id | user_id | photo | pro_pic |

|1 | 1 | Null | new.jpg |

|2 | 1 | 1.jpg | NULL |

|3 | 1 | new.gif| NULL |


So, when user creating a new post i have pass the user gallery id(where only if pro_pic exists).

My issue is when user didn’t upload any picture I’m getting the ‘NULL’ so I can’t upload the post.



	$criteria = new CDbCriteria();


	$criteria->condition="user_id = :user_id AND uploaded_picture NOT IN('NULL')";

	$criteria->params = array(

							':user_id' => Yii::app()->user->id


	$userGallery = UserGallery::model()->find($criteria);

	$imageFile = empty($userGallery->id)?  'NULL':$userGallery->id;

        echo $form->textField($model,'pro_pic_id', array('value'=>$imageFile), array('size'=>20,'maxlength'=>20)); ?>

i think now its clear one :(

Or is any-other way to get the user profile picture from the user_gallery table to forum_post table?

for that purpose the default avatars are used for example if the user is a newbie and during registration he did not upload or set any profile picture so that you can assign him a default profile picture as like in the facebook

consider this scenario if the user does not upload any picture assign some default avatar so that your problem will be solved even if the user set’s the profile picture or not. you can create post ans you can send user profile picture without any problem