从yii2中的函数获取多个值 [英] Get multiple values from a function in yii2

查看:303
本文介绍了从yii2中的函数获取多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Model 中有一个函数,它传递了一个值,并且在其中有一个查询给我两个结果。

  public static function updImgtbl($ id)
{
$ total_images = 0;
$ images_uploaded = 0;

$ sqlquery =SELECT SUM(CASE WHEN ims.`installation_id` = $ id THEN 1 ELSE 0 END)AS total_images,
SUM(CASE WHEN ims.`image_upload_flag` = 1 AND ims.`installation_id` = $ id THEN 1 ELSE 0 END)AS images_uploaded
FROM`installation_images_site` ims
INNER JOIN`installed` ins ON ins.`id` = ims.`installation_id`
WHERE(ims.`image_upload_flag` = 1 AND ims.`installation_id` = $ id)
OR(ims.`installation_id` = $ id)
GROUP BY ims.`installation_id`;

$ pt = Yii :: $ app-> db-> createCommand($ sqlquery) - > queryAll();

foreach($ pt as $ val)
{
$ total_images = $ val ['total_images'];
$ images_uploaded = $ val ['images_uploaded'];
}
return [total_images=> $ total_images,images_uploaded=> $ images_uploaded];

$ / code>

这个函数是从 API $ b $ $ p $ $ response = Installations :: updImgtbl($ install_id);
print_r($ response);
exit();

反应如下

  Array 

[total_images] => 4
[images_uploaded] => 1

但我想在API中的变量中获取这些值。如何才能实现它?

解决方案

您可以简单地:

  list($ total_images,$ images_uploaded)= array_values($ response); 

然后,您的 total_images code> $ total_images 变量和 images_uploaded $ images_uploaded 变量中。 p>

我的意思是:
$ b $

  list($ total_images,$ images_uploaded)=安装:: updImgtbl($ install_id); 
echo $ total_images; // 4
echo $ images_uploaded; // 1

您可以阅读更多关于 list 官方文档中。

另一种方法是使用 extract()函数。例如:

  $ response = Installations :: updImgtbl($ install_id); 
提取($ response);
echo $ total_images; // 4
echo $ images_uploaded; // 1


I have a function in my Model which is passed a single value and in which I have a query which is giving me two results.

 public static function updImgtbl($id)
{
    $total_images = 0;
    $images_uploaded = 0;

    $sqlquery = "SELECT  SUM(CASE WHEN ims.`installation_id` = $id THEN 1 ELSE 0 END) AS total_images, 
    SUM(CASE WHEN ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id THEN 1 ELSE 0 END) AS images_uploaded
    FROM    `installation_images_site` ims
    INNER JOIN `installations` ins ON ins.`id` = ims.`installation_id`
    WHERE   (ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id)
    OR (ims.`installation_id` = $id) 
    GROUP BY ims.`installation_id`";

    $pt = Yii::$app->db->createCommand($sqlquery)->queryAll();

    foreach ($pt as $val)
    {
        $total_images= $val['total_images'];
        $images_uploaded = $val['images_uploaded'];
    }
    return ["total_images" => $total_images, "images_uploaded"=>$images_uploaded];
}

This function is called from an API

 $response = Installations::updImgtbl($install_id);
        print_r($response);
        exit();

The response is below

Array
(
  [total_images] => 4
  [images_uploaded] => 1
)

But I want to get these values in a variable in my API. How can I achieve it?

解决方案

You can simply:

list($total_images, $images_uploaded) = array_values($response);

Then, you have total_images value in $total_images variable and images_uploaded in $images_uploaded variable.

I mean:

list($total_images, $images_uploaded) = Installations::updImgtbl($install_id);
echo $total_images; // 4
echo $images_uploaded; // 1

You can read more about list in official doc.

Another way is using extract() function. For example:

$response = Installations::updImgtbl($install_id);
extract($response);   
echo $total_images; // 4
echo $images_uploaded; // 1

这篇关于从yii2中的函数获取多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆