Echoing Cache Values

I am having some trouble working with cache values. My code is as follows:

1  $size=Yii::app()->cache->get('imagesize');


2  if ($size===false) {


3  	echo("size value is NOT set<br />");


4  	$model = Settings::model()->findByPk('DefaultImageSize');


5  	$cache['imagesize'] = $model->value;


6  } else {


7  	echo("size value IS set <br />");


8  	echo($cache['imagesize']);


9  }

It errors on line 8 - "Object of class Settings could not be converted to string". So clearly the variable $size is set, but I am unable to echo that value. I am a noobie, so I must be missing something very simple. Can anyone see what I am not seeing?

Many thanks!

The errors tells you $cache[‘imagesize’] is a object or an array,you can use var_dump($cache[‘imagesize’]) to check the value.

Thanks so much, lenye. That resolves that issue. But now, when I want to use that value to concatenate it to another variable (or something), how do I do that? I run into the same issue.

Thanks for your help.

i don’t understand what you want exactly,could you please give me more issues? :lol:

Sure…

Now I want to use something like:

$newVariable = “some string”.$cache[‘imagesize’];

and I get the same error.

Thanks again.

your $cache[‘imagesize’] is not a string , so you can’t concatnate it with dot. please print_r($cache['imagesize]),and paste your code. :rolleyes:

I get:

Settings Object( [_md:CActiveRecord:private] => [_new:CActiveRecord:private] => [_attributes:CActiveRecord:private] => Array ( [setting] => DefaultImageSize [value] => 640 ) [_related:CActiveRecord:private] => Array ( ) [_c:CActiveRecord:private] => [_pk:CActiveRecord:private] => DefaultImageSize [_errors:CModel:private] => Array ( ) [_validators:CModel:private] => [_scenario:CModel:private] => update [_e:CComponent:private] => [_m:CComponent:private] => )

Thanks again.

Thanks again, lenye,

I think I resolved my problem. The issue was not the retrieval of the cache values, it was that it was never set correctly in the first place. I replaced the "if ($value===false) {…" with "if (isset($value)) {…" and no everything works just fine.

Thanks so much for your assistance!