yiibjorn
(Bjorn)
1
Hi,
I’m not sure what a good approach is for loading default settings into my model.
I have a model Image that extends CActiveRecord. It’s based on my table ‘image’ that hold all the information about uploaded images.
In this model I have a method that generates thumbnails. And i want to define the width and height of them in my main config.
In config/main.php I have something like:
'params'=>array(
'image'=>array(
'thumbnail_sizes'=>array(
array('width'=>235,'height'=>null),
array('width'=>350,'height'=>null),
array('width'=>705,'height'=>1200)
),
In my model:
public $thumbnail_sizes;
function __construct()
{
$this->thumbnail_sizes = Yii::app()->params['image']['thumbnail_sizes']
}
Is this how I should do this or is there a better way?
Hi,
I think you can put the variable directly. Why we should over ride the __construct?
Cheers!
yiibjorn
(Bjorn)
3
Hi, how do you mean directly?
I’m still a beginner you know
.
if the thumbnail_sizes is a fixed constant means
You can write the code directly as follows
class yourmodelclass extends CActiveRecord{
public $thumbnail_sizes = Yii::app()->params['image']['thumbnail_sizes'];
//Blah... blah...
is enough.
yiibjorn
(Bjorn)
5
I thought this was not possible and indeed my editor gives me a syntax error…
Somebody could explain this to me?
hemc
(Hemendra Chaudhary619)
6
You can also use init() method for set variable.
public $thumbnail_sizes;
public function init()
{
$this->thumbnail_sizes= Yii::app()->params['image']['thumbnail_sizes'];
}
yiibjorn
(Bjorn)
7
Thanks Hemc, I will try that!