首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将变量从模型访问到控制器?

如何将变量从模型访问到控制器?
EN

Stack Overflow用户
提问于 2018-01-26 13:27:28
回答 1查看 983关注 0票数 0

我正在研究yii2。我遇到过这样一种情况:当安装了仪表并将其图像上传到服务器时,我必须向某人发送电子邮件。为此,我已经配置了swift mailer

有一个名为Installations的模型,它具有保存所有安装数据的功能。

代码语言:javascript
复制
public static function saveAll($inputs){
    $coutner = 0;
    $arr_status = [];

    foreach ($inputs as $input) {
        $s = new Installations;
        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 (Installations::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->istallation_status == 'Installed') {

                Meters::change_status_byinstall($s->meter_msn, Meters::$status_titles[4]);
            }
            else if ($s->istallation_status != 'Installed' && $s->comm_status =='Failed')
            {
                Meters::change_status_byinstall($s->meter_msn, Meters::$status_titles[5]);
            }
            $arr_status[] = ['install_id' => $input->install_id, 'status' => 1];
            $coutner++;

            if (isset($input->doc_images_name)) {
                foreach ($input->doc_images_name as $img) {
                    $image = new InstallationImages;
                    $image->image_name = $img->image_name;
                    $image->installation_id = $s->id;
                    $image->save();
                }
            }
            if (isset($input->site_images_name)) {
                foreach ($input->site_images_name as $img2) {
                    $image2 = new InstallationImagesSite;
                    $image2->image_name = $img2->image_name;
                    $image2->installation_id = $s->id;
                    $image2->save();
                }
            }
        }else{
            $arr_status[] = ['install_id' => $input->install_id, 'status' => 0, 'messages' => $s->errors];
        }
        $status = $s->istallation_status;
        $msn = $s->meter_msn;
        $com = $s->comm_status;
        // want to pass these variables to the controller function

    }

    return ['status' => 'OK', 'details' => $arr_status, 'records_saved' => $coutner];

}

现在有一个名为InstallationController的控制器。这个控制器包含我的移动应用程序的所有APIs。下面是其中的两个主要函数

代码语言:javascript
复制
public function actionAddnew()
{
    $fp = fopen('debugeeeeeee.txt', 'w+');
    fwrite($fp, file_get_contents('php://input'));
    fclose($fp);
    $inputs = json_decode(file_get_contents('php://input'));

    return Installations::saveAll($inputs);
}

 public function actionSavephoto()
{


    try {
        $count = 0;
        foreach ($_FILES as $f) {
            $dd = pathinfo($f['name']);
            if (!isset($dd['extension']) || !in_array($dd['extension'], array('jpg', 'png', 'gif'))) {
                return ['status' => 'ERROR', 'uploaded_files' => $count, 'message' => 'Invalid File'];
                break;
            }
            if (move_uploaded_file($f['tmp_name'], Installations::UPLOAD_FOLDER . $f['name'])) {
                $count++;

                return ['status' => 'OK', 'uploaded_files' => $count];

                break;
            } else {
                return ['status' => 'ERROR', 'uploaded_files' => $count];
                break;
            }
        }

    } catch (Exception $x) {
        return ['status' => 'ERROR', 'message' => $x->getMessage()];
    }
}

移动应用程序将调用Addnew()应用程序接口,之后它将调用savephoto。现在,我想将$msn$status$com值从模型传递到控制器函数Savephoto

为此,我尝试使用session variables,但仍然无法获得所需的结果。

我还检查了问题Yii, how to pass variables to model from controller? ,但它不适用于我。

我怎样才能做到这一点?

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-26 14:16:03

saveAll()中获取这些值的唯一方法是返回它们。目前,它们是在$s中的一个对象上定义的,每个循环都会被覆盖。最好的方法似乎是在foreach ($inputs...循环之外创建一个数组,并附加每个创建的Installations对象。

在最后返回它,并将它(或其中的相关元素)作为参数传递到actionSavephoto()中。然后,这些值将可以通过传递的对象的属性进行访问。此处理将发生在未图示的代码中,该代码先调用actionAddNew(),然后调用actionSavephoto()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48456211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档