在模型函数中返回两个单独的查询结果 [英] Returning two separate query results within a model function

查看:117
本文介绍了在模型函数中返回两个单独的查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型有一个函数应该运行2个不同的查询。

My model has a function that should run 2 different queries.

理想情况下,我想它返回2个不同的vars到我的控制器。

Ideally I would like it to return 2 different vars (1 from each query) that would be available to my controller.

下面的代码显然不工作b / c第一个return结束执行就在那里。但是我怎么能在同一个函数中生成这两个结果呢?

The code below obviously doesn't work b/c the first return ends execution right there. But how could I do something that would generate these two results within the same function?

感谢您提供的任何帮助/指针。

Thanks for any help / pointer you can provide.

MODEL -- Hypothetical code

function ABC() 
{
    $query1 = $this->db->query( ... MySQL code #1 here ... );

    $data1 = array();
    foreach (query1->result() as $row){
        $data1[$row->k] = $row->value;

    return $data1;    

    $query2 = $this->db->query( ... MySQL code #2 here ... );

    $data2 = array();
    foreach (query2->result() as $row){
        $data2[$row->k] = $row->value;

    return $data2;
}


推荐答案

建议,但你的类/方法设计似乎有一个问题。你将更好地重构这个代码,并将结果返回两个不同的函数。

You can do this like fazo suggested but your class/method design seems to have a problem. You would be better of refactoring this code and return the results in two different functions.

这篇关于在模型函数中返回两个单独的查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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