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 :


countryID

Citries Table fields :


cityID

country_ID

the phrases table


phraseKey

phraseValue

phraseLang

& 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,


userID

username

country

city

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 users.country=countries.countryID) 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 users.city=cities.cityID) as city from users

Thanks

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');

//Translating

$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


phraseKey

phraseKeyID

phraseValue

phraseLang

& 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

Adnan