Yii2-如何从模型访问变量到控制器?

时间:2018-01-26 05:27:28

标签: variables yii model controller yii2

我正在研究swift mailer。我遇到了一个问题,当安装仪表并将图像上传到服务器时,我必须向某人发送电子邮件。我已经配置了Installations

有一个名为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。该控制器包含我的移动应用程序的所有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 api,之后将调用$msn。现在,我想将模型中的$status$comSavephoto值传递给控制器​​函数session variables

为此,我尝试使用$(".signupbtn").on('click', function(){ $('input:text').each(function(){ if ($(this).val().length == 0){ } else { $(this).hide(); //What I should add here? } }); });,但仍然无法获得所需的结果。

我还检查了Yii, how to pass variables to model from controller? 这个问题,但它对我没用。

我怎样才能实现它?

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

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

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