Activerecord Relations Has_One With Text Field

Hi this, first time i use Yii,

i have a 3 tables,

one for countries,cities, the other for phrases.

Countries Table fields :


Citries Table fields :



the phrases table




& i get the names from the phrases

the phraseKey is like that ‘country:’+countryID or 'city:'cityID

before i was get it like that

select *,phrases.phraseValue as countryTitle from countries,phrases where phraseKey=CONCAT('country:', countries.countryID) AND phraseLang='{CURRENT_LANUAGE_OF_THE_WEBSITE}'

so how can i do the same in Yii

in the Activerecord relations ? & how ?

is there any way more easy in Yii ? how?

what if i want to get a country title & city title in the same query in Yii ?

i can do it like that

for example i have a table for users,





the sql :

select users.*,(select phrases.phraseValue as countryTitle from user,countries,phrases where phraseKey=CONCAT('country:', countries.countryID) AND phraseLang='{CURRENT_LANUAGE_OF_THE_WEBSITE}' AND as country,(select phrases.phraseValue as cityTitle from user,cities,phrases where phraseKey=CONCAT('city:', cities.cityID) AND phraseLang='{CURRENT_LANUAGE_OF_THE_WEBSITE}' AND as city from users


I assume you would like to translate the name of the city / country? What we prefer in that case is querying the country- (or city-) name and use Yii’s messaging for translating either the cityname or countryname afterwards.

Welcome to Yii btw!

thank you for your replay :)

can you give me an example to use the messaging for translation?

because for example the country title will be more than one record in the phrases table :

for example Canada

phraseKey phraseValue phraseLang

country:1 Canada en

country:1 Kanada tr

country:1 كندا ar

country:1 カナダ jp

Thanks again,

i hope we will be friends because you are first person gave me an answer in Yii :D

Take a look at the t function. This will do the trick. A short example:

//Retrieving your country

$country = Country::model()->findByPk('nl');


$countryName = Yii::t('countries', $country->name);

Make sure you have something like this in messages/countries/nl.php (for example):

return array(

  'Germany' => 'Duitsland',

  'the Netherlands' => 'Nederland',


Thanks for all your support my brother,

i create a solution,

i changed the the phrases table

to by like that





& after that i create a HAS_ONE relation between the countryID & phraseKeyID, & i add a condition phraseKey = country & phraseLang = current language,

and your answer was beneficial too,

i recently started and this my first project using Yii

thanks alot