SQL problems in Yii...find...from.. where++Yii-user

Hi everyone,

I am currently using yii-user.

And I do have a field called country (generated by yii-user profile fields)

And I also have a database Country.

My question is. How can I access the country_id through the "profileController"

For example. in my profile page, we can see

Username:admin

Country: U.S.A

I want to add a button like "click to login to your country"

So I need to access that user’s country id here.

What should I do?

I tried this: but give me a syntax error:





private $_country = null;


 public function getCountry()

        {

      

          $user=Yii::app()->user->id;         

          $_country= Yii::app()->db->createCommand('select country_id FROM tbl_profiles WHERE user_id=$user')->queryAll();  


           

        return $this->_country;

        }



I guess I can’t use $user in SQL query…

What should i do to access the user’s country id anyway???

These information is stored in my database “tbl_profiles” But i don’t know how to access them…So sad…

Thanks so much for your help!!!

this works?


  $profile=Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id,));

  echo $profile->country_id;  //U.S.A.



Thanks for your help:

I tried your code but not works… and I tried this way:


        private $_country = null;


      protected function loadCountry($country_id)

        {

       if ($this->_country === null) {

       $this->_country=Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));

       }

       return $this->_country;

        }


 public function getCountry()

	{

        return $this->_country;

	}

and in my view:


<?php  

$this->menu=array(

        array('label'=>'Your Country', 'url'=>array('../index.php/', 'country'=>$this->getCountry()->country_id)),

);    

?>

It gives me an ERROR:

"Trying to get property of non-object"

What should I do…

And why I can access the university_id through your code…? I don’t quite understand…

Thanks for your help…I really appreciate it.

a bit confusing about your loadCountry function, or maybe i miss some points (still new to yii)

how about this?




public function getCountry()

{

	if ($this->_country === null) {

		$this->_country=Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));

	}

	return $this->_country;

}



Thanks soooooo much man.

That works perfectly.

Thanks man!