如何解决“类Closure的对象无法转换为int”

时间:2017-10-29 06:52:14

标签: php yii2

我打算在gridview中调用模型中的数据。我通过composer安装了高级网格视图,我正在使用\ yiister \ grid \ widgets \ ProgressColumn。但是prolem ProgressColumn小部件没有采用

 'value'=>function($model){
          return $model->paxtashart;
         },

作为int值,它给出了错误:类Closure的对象无法转换为int

我是否有可能将其转换为int或任何其他替代方案?

在这里,让我向您介绍我的全部代码:

  [


        'class' => \yiister\grid\widgets\ProgressColumn::className(),
        'attribute' => 'paxta_given',
        'size' => \yiister\grid\widgets\ProgressColumn::SIZE_LARGE,
        'isAnimated' => true,
         'value'=>function($model){
           $model->paxtashart;
         },

        // 'maxValue'=> $fermercha->paxtashart,
        // 'minValue'=> $fermercha->paxta_given,
        'progressBarClass' => function ($model, $column) {
            return $model->{$column->attribute} > 60
                ? \yiister\grid\widgets\ProgressColumn::STYLE_SUCCESS
                : \yiister\grid\widgets\ProgressColumn::STYLE_WARNING;
        },
    ],

我试过这样的方式:     $模型 - > paxtashart 但它是jsut给我一个值所有属性,它没有帮助 这是我的模型:

 <?php

    namespace app\models;

    use Yii;

    /**
     * This is the model class for table "fermer".
     *
     * @property integer $id
     * @property string $FIO
     * @property integer $tuman_id
     * @property string $Massiv
     * @property integer $ferhojalik
     * @property integer $maydoni
     * @property integer $paxtamay
     * @property integer $paxtashart
     * @property integer $gallamay
     * @property integer $gallashart
     * @property integer $bog
     * @property integer $uzum
     * @property integer $poliz
     * @property integer $sabzavot
     * @property integer $chorva
     */
    class Fermer extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'fermer';
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [

                [['tuman_id', 'ferhojalik', 'maydoni', 'paxtamay', 'paxtashart', 'gallamay', 'gallashart', 'paxta_given', 'poliz', 'sabzavot', 'chorva','paxta_done'], 'integer'],
                [['FIO', 'Massiv'], 'string', 'max' => 255],
                [['image'], 'file',  'extensions' => ['png','jpg','jpeg']],

            ];
        }

        public function upload()
        {
            if ($this->validate() and $this->image->baseName) {
                $this->image->saveAs(Yii::$app->basePath.'/web/uploads/' . $this->image->baseName . '.' . $this->image->extension);
                return true;
            } else {
                return false;
            }
        }
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => 'ID',
                'FIO' => 'ФИО',
                'tuman_id' => 'Туман номи',
                'Massiv' => 'Массив номи',
                'ferhojalik' => 'Фермер хўжалиги номи',
                'maydoni' => 'Майдони',
                'paxtamay' => 'Пахта майдони',
                'paxtashart' => 'Пахта майдони шартнома режаси (тонна)',
                'gallamay' => 'Ғалла майдони',
                'gallashart' => 'Ғалла майдони шартнома режаси (тонна)',
                'paxta_given' => 'Пахта шартнома бўйича топширилган тонна',
                'image' => 'Расм',
                'poliz' => 'Полизчилик',
                'sabzavot' => 'Сабзавотчилик',
                'chorva' => 'Чорвачилик',
            ];
        }
            // return $this->hasone(Extraagri::className(), ['id' => 'po_item_no']);

     public function getDone()
     {
        return  $this->hasone(done::classname(),['id'=>'paxta_done']);
     }

    /*      public function getPoitem()
        {
            return $this->hasMany(Poitem::className(), ['id' => 'po_item_no']);
        }*/

     /* public function getExtraagri()
        {
            return $this->hasMany(Extraagri::className(), ['id' => 'id']);
        }
    */


    }

1 个答案:

答案 0 :(得分:1)

匿名函数是Class闭包的一个实例。尝试以下变化

    //assign the closure to variable
    $value = function($model){
           return $model->paxtashart;
         }  
    $progressBarClass = function ($model, $column) {
            return $model->{$column->attribute} > 60
                ? \yiister\grid\widgets\ProgressColumn::STYLE_SUCCESS
                : \yiister\grid\widgets\ProgressColumn::STYLE_WARNING;
        }   
    [
        'class' => \yiister\grid\widgets\ProgressColumn::className(),
        'attribute' => 'paxta_given',
        'size' => \yiister\grid\widgets\ProgressColumn::SIZE_LARGE,
        'isAnimated' => true,
        'value'=>(int)$value($model), //invoke the closure and cast as integer
        'progressBarClass' =>(string)$progressBarClass($model, $column), //invoke the closure and cast as string
    ],