Yii2在非对象上调用成员函数getImageUrl()

时间:2015-07-28 02:57:50

标签: php function yii2

我有一个显示数据的GridView,其中一个是图像。这是我GridView中的代码:

echo GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'fullName',
        [
            'header' => '<a style="cursor: pointer;">Attachment</a>',
            'format' => 'html',
            'value' => function ($data) {
                return Html::img($data->getImageUrl(), ['class' => 'reim-attach']);
            },
        ],
        'receipt_company',
        'description',
        'date',
        'amount',
        [
            'attribute' => 'chargeable',
            'value' => function ($model) {
                return $model['chargeable'] ? 'Chargeable' : 'Non-chargeable';
            },
        ],
        'GST_amount',
        'date_noted',
        [
            'attribute' => 'status',
            'label' => 'Status',
            'content' => function ($model, $key, $index, $column) {
                if ($model['status'] == "Pending") {
                    return Html::button('Pending', ['class' => 'status-pending']);
                } elseif ($model['status'] == "Draft") {
                    return Html::button('Draft', ['class' => 'status-pending']);
                } elseif ($model['status'] == "Approved") {
                    return Html::button('Approved', ['class' => 'status-approved']);
                } else {
                    return Html::button('Rejected', ['class' => 'status-rejected']);
                }
            }
        ],
        [
            'label' => 'Action',
            'content' => function ($model, $key, $index, $column) {
                if($model['status'] == "Pending") {
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' =>  $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve', 'data' => [ 'confirm' => 'Are you sure you want to approve this reimbursement?', 'method' => 'post', ]])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-ban"></i> Reject', ['value' =>  $model['_id'], 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject', 'data' => [ 'confirm' => 'Are you sure you want to reject this reimbursement?', 'method' => 'post', ]]);
                }  elseif($model['status'] == "Draft") {
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-check-circle-o"></i> Save Reimbursement', ['value' =>  $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'saveReimbursement(value)', 'data-toggle'=>'tooltip','title'=>'Save Reimbursement', 'data' => [ 'confirm' => 'Are you sure you want to save this reimbursement?', 'method' => 'post', ]]);
                } else {
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve', 'data' => [ 'confirm' => 'Are you sure you want to approve this reimbursement?', 'method' => 'post', ]])
                    .'&nbsp'
                    .Html::button('<i class="fa fa-ban"></i> Reject', ['value' =>  $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject', 'data' => [ 'confirm' => 'Are you sure you want to reject this reimbursement?', 'method' => 'post', ]]);
                }
             }
        ]
    ],
]); 

我在第return Html::img($data->getImageUrl(), ['class' => 'reim-attach'])行上收到以下错误:

  

在非对象

上调用成员函数getImageUrl()

这是控制器的片段:

$model = new Reimbursement();
$reimbursementQuery = new Query;
$reimbursementQuery->select([])->from('reimbursement')->andwhere(['company_id' => new \MongoId($session['company_id'])]);

if (isset(Yii::$app->request->getBodyParams()['period'])) {
    if(Yii::$app->request->getBodyParams()['period'] != '') {
        echo Yii::$app->request->getBodyParams()['period'];
        $refreshData = true;
        $selectedPeriodID = Yii::$app->request->getBodyParams()['period'];
        $selectedPeriod = Periods::find()->where(['_id' => $selectedPeriodID])->one();
        $reimbursementQuery->andWhere(['date_reimbursed' => array('$gte' => date('Y/m/d', strtotime($selectedPeriod->p_start)), '$lte' => date('Y/m/d', strtotime($selectedPeriod->p_end))) ]);
    }
}

$reimbursements = $reimbursementQuery->all();
$dataProvider = new ArrayDataProvider([
    'allModels' => $reimbursements,
]);            

if($refreshData) {
    return $this->renderPartial('_reports', [
        'dataProvider' => $dataProvider, 
    ]);
}

$dataProvider->pagination->pageSize = 10; 
return $this->render('reports', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,
    'model' => $model,
    'rModel' => $rModel,
    'employees' => $employees,
    'contacts' => $contacts,
    'periods' => $periods,
]);   exit;

这里也是我的模型

public function getImageUrl()
{        
    return Url::to('@web/' . $this->attachment, true);
}

希望有人能指出我错过的地方以及如何摆脱错误。

1 个答案:

答案 0 :(得分:0)

它的发生是因为你没有访问数据库中的任何数据,或者你没有指定你的功能。 。

[ 'header' => '<a style="cursor: pointer;">Attachment</a>', 'format' => 'html','value' => function ($data) {
          $attachment = YourModel::find($model->yourId)->one()->attachment;
        return Html::img(Url::to('@web/' . $attachment, true), ['class' => 'reim-attach']);
        },
    ],