Yii2 - 无法通过客户端界面

时间:2018-03-29 05:15:58

标签: yii2 client postman yii2-advanced-app save-image

我正在研究Yii2。我开发了API,它将保存来自client的传入记录。客户端可以发送image,因此我保存其名称,然后在文件夹中上传。除了图像的名称,我也用它发送数据。保存除image name以外的所有数据。以下是我的API电话。

public function actionAddnew()
{
    $fp = fopen('preinstall.txt', 'w+');
    fwrite($fp, file_get_contents('php://input'));
    fclose($fp);
    $inputs = json_decode(file_get_contents('php://input'));

    return PreInstallations::saveAll($inputs);
}

函数saveAll

 public static function saveAll($inputs)
{

    $coutner = 0;
    $arr_status = [];
    foreach ($inputs as $input) {
        $s = new PreInstallations;
        foreach ((array)$input as $key => $value) {
            if ($key != 'image_names') {
                if ($s->hasAttribute($key)) {
                    $s->$key = $value;

                }
            }

        }

        $user = Yii::$app->user;

        if (isset($input->auth_key) && Users::find()->where(['auth_key' => $input->auth_key])->exists()) {
            $user = Users::find()->where(['auth_key' => $input->auth_key])->one();
        }

        $s->created_by = $user->id;

        if (PreInstallations::find()->where(['ref_no' => $input->ref_no])->exists()) {
            $arr_status[] = ['install_id' => $input->install_id, 'status' => 2, 'messages' => "Ref # Already exists"];
            continue;
        }

        $s->sync_date = date('Y-m-d H:i:s  ');

        if ($s->save()) {
            if ($s->pre_install_status == 'Pre Installed') {
                Meters::change_status_byinstall($s->meter_msn, Meters::$status_titles[8]);
            }

            $arr_status[] = ['install_id' => $input->install_id, 'status' => 1];
            $coutner++;

            if (isset($input->site_images_name)) {
                foreach ($input->site_images_name as $img) {
                    $image2 = new PreInstallationImagesSite;
                    $image2->image_name = $img->image_name;
                    $image2->pre_install_id = $s->id;
                    $image2->save();
                }
            }
        }

        else {
            $arr_status[] = ['install_id' => $input->install_id, 'status' => 0, 'messages' => $s->errors];
        }
    }
    return ['status' => 'OK', 'details' => $arr_status, 'records_saved' => $coutner];
}

以上部分代码保存图像名称

if (isset($input->site_images_name)) {
                foreach ($input->site_images_name as $img) {
                    $image2 = new PreInstallationImagesSite;
                    $image2->image_name = $img->image_name;
                    $image2->pre_install_id = $s->id;
                    $image2->save();
                }
            }

出于测试目的API来自POSTMAN的电话有以下详细信息

[{
"ref_no": "20371521444700U",
"meter_msn"      :"002999001189",
"billing_msn" : "74516" ,
"latitude": "36.2703169",
"longitude": "78.178266",
"tarrif": "07",
"s_load": "07",
"customer_id" :"37010673625",
"pre_install_status": "Pre Installed",
"site_issues" : "No issue",
"install_id" : "20371521444700U_1521263491",
"created_date": "2018-03-17 10:12:12",
"consumer_name": "HDA PUMPING STATION SEVERAGE UNO.8 LATIFABAD HYD",
"consumer_address" :"HDA PUMPING STATION SEVERAGE UNO.8 LATIFABAD HYD",
"auth_key": "key",// replaced the original auth key with key
"ct_ratio": "200/5",
"ct_ratio_quantity":"4",
"cable_type":"37/83",
"cable_length":"11",
"atb_installed": "Yes",
"meter_type":"L.T.TOU",
"site_images_name":["20371521444700U_1522299780_site_1.jpg"]
}]

当我Send请求时,我收到错误

{
"name": "PHP Notice",
"message": "Trying to get property of non-object",
"code": 8,
"type": "yii\\base\\ErrorException",
"file": "E:\\xampp\\htdocs\\inventory-web\\common\\models\\PreInstallations.php",
"line": 137,
"stack-trace": [
    "#0 E:\\xampp\\htdocs\\inventory-web\\common\\models\\PreInstallations.php(137): yii\\base\\ErrorHandler->handleError(8, 'Trying to get p...', 'E:\\\\xampp\\\\htdocs...', 137, Array)",
    "#1 E:\\xampp\\htdocs\\inventory-web\\api\\modules\\v1\\controllers\\PreinstallationController.php(36): common\\models\\PreInstallations::saveAll(Array)",
    "#2 [internal function]: api\\modules\\v1\\controllers\\PreinstallationController->actionAddnew()",
    "#3 E:\\xampp\\htdocs\\inventory-web\\vendor\\yiisoft\\yii2\\base\\InlineAction.php(57): call_user_func_array(Array, Array)",
    "#4 E:\\xampp\\htdocs\\inventory-web\\vendor\\yiisoft\\yii2\\base\\Controller.php(156): yii\\base\\InlineAction->runWithParams(Array)",
    "#5 E:\\xampp\\htdocs\\inventory-web\\vendor\\yiisoft\\yii2\\base\\Module.php(523): yii\\base\\Controller->runAction('addnew', Array)",
    "#6 E:\\xampp\\htdocs\\inventory-web\\vendor\\yiisoft\\yii2\\web\\Application.php(102): yii\\base\\Module->runAction('v1/preinstallat...', Array)",
    "#7 E:\\xampp\\htdocs\\inventory-web\\vendor\\yiisoft\\yii2\\base\\Application.php(380): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
    "#8 E:\\xampp\\htdocs\\inventory-web\\api\\web\\index.php(35): yii\\base\\Application->run()",
    "#9 {main}"
]
}

137行是$image2->image_name = $img->image_name;

Image model

 public function rules()
{
    return [
        [['pre_install_id'], 'required'],
        [['pre_install_id', 'image_upload_flag'], 'integer'],
        [['image_name'], 'string', 'max' => 255],
        [['pre_install_id'], 'exist', 'skipOnError' => true, 'targetClass' => PreInstallations::className(), 'targetAttribute' => ['pre_install_id' => 'id']],
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'pre_install_id' => 'Pre Install ID',
        'image_name' => 'Image Name',
        'image_upload_flag' => 'Image Upload Flag',
    ];
}

我必须遗漏一些我不知道的事情。任何帮助都将受到高度赞赏。

0 个答案:

没有答案