In the form of a model, need to use the dropdown list. The values for dropdown list selected from database table of another model. Now i want to use the cache in this. Storing the drop list values and use that cache in the form of drop down list. I read the wiki in Yii site but not clear about which method is used for that. When i can perform this?
In your second model, you could create a method to return the drop down list options and handle caching within that. Something like the following (with more appropriate names and keys):
public static function getOptions()
{
$result = Yii::app()->cache->get('ClassName_Options');
if (!$result)
{
$result = getOptionsHere();
Yii::app()->cache->set('ClassName_Options', $result, 3600);
}
return $result;
}
Note that you’ll have to heavily modify this depending on how you populate the options. The function may or may not need to be static and your cache key may have to have additional identifying information.
It’s up to you to get the return value into a form suitable for dropdown list options. I suspect that you’ll want to perform some sort of find request and use CHtml::listOptions() to get them into an associative array. This should be done as part of the getOptionsHere() step.
Treat the code above as a guide on how to solve the problem. You’ll need to adapt it for your own situation.
In my concept the country list is used in many forms of my application so for each form dropdown list i want to use this cache. A Global cache variable for used in every form drop down. Which way is better to implement this?
This is essentially global. The code to fetch the list should be in your Country model. This method is accessible from anywhere in your application by using Country::getOptions().
Please use a more appropriate method name than that though
In the Country model. Avoid putting global methods into controllers as not all controllers are accessible in every request; your base controller notwithstanding.