我正在研究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
。现在,我想将$msn
、$status
和$com
值从模型传递到控制器函数Savephoto
。
为此,我尝试使用session variables
,但仍然无法获得所需的结果。
我还检查了问题Yii, how to pass variables to model from controller? ,但它不适用于我。
我怎样才能做到这一点?
任何帮助都将不胜感激。
发布于 2018-01-26 06:16:03
从saveAll()
中获取这些值的唯一方法是返回它们。目前,它们是在$s中的一个对象上定义的,每个循环都会被覆盖。最好的方法似乎是在foreach ($inputs...
循环之外创建一个数组,并附加每个创建的Installations
对象。
在最后返回它,并将它(或其中的相关元素)作为参数传递到actionSavephoto()
中。然后,这些值将可以通过传递的对象的属性进行访问。此处理将发生在未图示的代码中,该代码先调用actionAddNew()
,然后调用actionSavephoto()
https://stackoverflow.com/questions/48456211
复制相似问题