Relations Problem

Hello Developers,

I m having problem with managing relations between two tables user and questions can anyone help me with this.

user table

userId

year,

question table

userId

question,

I have tried this

in question 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(


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


	);


}

in User 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(


                 'question' => array(self::HAS_MANY,'Question','userId'),


	);


}

and have tried all methods like findAll() and user::model()->with(‘user’)->findAll();

please ignore spelling mistakes

please REPLY SOON

thanks in advance

What do you want to get ?

all questions belong to User ?

just try :




   $user1 =  new User;

   $questions = $user1->question;

   var_dump($questions);



Hello

Yes all question belong to user

and what i need is all questions related to user and user details combined

Is not working my dear friend

Hi,

please see it…

and query like something


            $criteria = new CDbCriteria;

            $criteria->select = 't.*, tu.* ';

            $criteria->with = array('event' => array('with' => 'student_user'));

            $criteria->addCondition("username='".$this->user_id."'");

             $resultSet    =    Users::model()->findAll($criteria);

i think it’s may help…

please, show me more detail about database structure and model class




   user table

       userId

      year,


   question table

       userId

       question,

 

where 's the id field of question ?

I saw your relations method




   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(

'question' => array(self::HAS_MANY,'Question','userId'),

);

}




it’s no mistake (I think ) …

question table has

id, PK

userId, FK references user

question,

and user table has

userId, PK

years,

and I want combine data related to user and its questions

hope its clear but still not working :(

Its not working for me

Looking at your table layout (and assuming this is complete), each user can only have at most one question assigned to him. Is this intended?

Well, you keep saying “It’s not working” but you didn’t show what you have tried and what error message that you got if any. I think Chung Tran’s answer should solve your problem, but you said it’s not working, so what’s the error message or what’s the result that you think an error?

the error was related to pk alias name and the problem has been resolved Thanks all of you…