duanshangying5102 2015-01-11 17:10
浏览 55

从被调用函数访问时尝试获取非对象的属性

I'm using yii framework but I think this is related to PHP

In my controller, I have the following code

$model = new Events;
    $model->type_id = $type_id;
    $checkFileUpload = checkFileUpload($model);

the function checkFileUpload is a custom function which contains

function checkFileUpload($model)
    {
        $rnd = rand(0, 9999);
        $uploadedFile = CUploadedFile::getInstance($model, 'image');
        if($uploadedFile->error == 0)
        { 
            $fileName = "{$rnd}-{$uploadedFile}";  // random number  file name
            $model->image = $fileName;
...

I got the error get property of non-object in $uploadedFile->error.

I've tried to use reference to the model instead, but it is deprecated and does not work for me.

If I use the code of the called function (checkFileUpload) within the controller code, it works fine. I suspect that object is not passed in a correct way.

Any help?

  • 写回答

4条回答 默认 最新

  • dongyan1808 2015-01-11 17:17
    关注

    This is because your call to CUploadedFile::getInstance returns null and not the instance you desired.

    Null is returned if no file is uploaded for the specified model attribute.

    Yii Documentation

    It seems like your file was not correctly uploaded. I am not a Yii Framework user, but the documentation states:

    The file should be uploaded using CHtml::activeFileField.

    Yii Documentation

    So you should verify that the file was actually correctly uploaded with the proper method from the Yii Framework.

    PS: Objects are always passed by reference.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探