0

在我的控制器中,我有一个查询结果列表(最多 10 行左右)。我正在尝试使用查询的结果在另一个中使用。例如,第一个查询提取“agency_ID”(我希望在下一个查询中使用的数据)

控制器:

$data['list_report'] = $this->reports_model->filteredReport();
$data['area_data'] = $this->reports_model->getAreaDataReport($data['list_report']);

模型:

function getAreaDataReport($data)
{       
    $query = $this->db->query('SELECT COUNT(asa.sector_ID) AS area_count, asa.*, ags.sector
    FROM agencies_sector_association asa, agencies_sector ags WHERE agency_ID IN ('. $data->agency_ID .')
    AND asa.agency_ID = ags.ID
    GROUP BY agency_ID');
    return $query->result();
}

我收到此错误:

Message: trying to get property of non-object codeigniter model

当我 print_r 第一个查询结果时,我得到以下结果。

Array ( [0] => stdClass Object ( [case_ID] => 1 [reference_ID] => IFSOU001 [ID] => 1 [agency_ID] => 1 [agency_name] => Agency1 [task_ID] => 3 [task] => Domestic violence [outcome_ID] => 3 [outcome] => Protection plan no longer in place [creation_date] => 2012-12-05 ) [1] => stdClass Object ( [case_ID] => 3 [reference_ID] => NEW REFERENCE ID [ID] => 2 [agency_ID] => 1 [agency_name] => Agency1 [task_ID] => 1 [task] => Transport [outcome_ID] => 8 [outcome] => Bespoke needs based assessment received [creation_date] => 2013-05-01 ) [2] => stdClass Object ( [case_ID] => 6 [reference_ID] => REFERENCE ID [ID] => 6 [agency_ID] => 1 [agency_name] => Agency1 [task_ID] => 2 [task] => Counselling [outcome_ID] => 2 [outcome] => Improved school attendance [creation_date] => 2013-05-01 ) )

我的问题是如何在下一个语句中使用第一个语句的 Agency_ID 作为数组,这样它就不会只返回一行。从这个结果中,我得到了 3 个“agency_ID”结果,但在以后的搜索中可能会更多。

非常感谢所有可以提供帮助的人!

4

2 回答 2

1

$data是一个只包含一个对象的对象数组。

替换$data->agency_ID$data[0]->agency_ID

这将为您提供存储在数组中位置的对象的成员变量agency_ID[0]$data

假设您在结果数组中有多个对象,您需要获取 id 并将它们存储在字符串中,然后在查询中使用此字符串

$id_str = '';
foreach($data as $agency) {
    $id_str .= $agency->agency_ID . ', ';
}
$id_str = rtrim($id_str, ', '); //remove the trailing comma and space

现在$data->agency_ID在您的查询中替换为$id_str

于 2013-05-03T09:21:41.193 回答
0

您必须重新格式化结果数组,使其适合IN语句。格式如23, 17, 20. 应该$this->reports_model->filteredReport();返回.result_array()

$data['list_report'] = $this->reports_model->filteredReport();

$data_list = array();

foreach($data['list_report'] as $lp) {
    $data_list[] = $lp->agency_ID;
}

$data['area_data'] = $this->reports_model->getAreaDataReport(implode(',',$data_list));

然后只需在查询中使用该字符串:

function getAreaDataReport($data)
{       
    $query = $this->db->query('SELECT COUNT(asa.sector_ID) AS area_count, asa.*, ags.sector
    FROM agencies_sector_association asa, agencies_sector ags WHERE agency_ID IN ('.$data.')
    AND asa.agency_ID = ags.ID
    GROUP BY agency_ID');
    return $query->result();
}
于 2013-05-03T09:28:45.103 回答