简体   繁体   中英

Yii, how to pass variables to model from controller?

im wondering how to do it, i dont think its the same way about passing variables from controller to views, its just that i need to count columns on the database and i can do it from the controller, but how to pass it to model?.

public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'parametro_id' => 'Parametro',
            'variable' =>  'ucfirst($variable)',    
        );
    }

variable is the var, it says its not defined, how to do it?

Yii will automatically generate a label using generateAttributeLabel based on the column name in the database if you do not specify one. You do not need to call ucfirst() to format your variable.

By default an attribute label is generated using generateAttributeLabel.

Source: http://www.yiiframework.com/doc/api/1.1/CModel#attributeLabels-detail


generateAttributeLabel() method:

Generates a user friendly attribute label. This is done by replacing underscores or dashes with blanks and changing the first letter of each word to upper case. For example, 'department_name' or 'DepartmentName' becomes 'Department Name'.


Simply remove that line in your labels method to allow Yii to format the name for you:

'variable' => 'ucfirst($variable)',

You can Declare a variable in model as

public $columnCount;

Now in controller you can count the model and you can create an object for particular model and later on assign the value to this

public function actionColumncount(){
    $model = new modelname();
    $model->columnCount = 5;//Your count operation goes here
    //You may proceed with your proceedings
}

Then in model you can access this as

$this->columnCount;

Hope this would do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM