您可以使用一些连接和 GROUP_BY 子句轻松地将这几个查询转换为一个查询。这种方法最大的缺点是它需要对数据进行一点分组和定界,但 PHP 擅长“爆炸”定界数据。试试这个:
function all()
{
$this->db->select("`modules`.*, CONCAT(`layouts`.`id`,'".DATA_SUBDELIMITER."',`layouts`.`title`,'".DATA_SUBDELIMITER."',`layouts`.`file`) AS layout, CAST(GROUP_CONCAT(`sections`.`id`,'".DATA_SUBDELIMITER."',`sections`.`title` SEPARATOR '".DATA_DELIMITER."') AS CHAR) AS sections", false);
$this->db->from('modules');
$this->db->join('layouts', 'layouts.id = modules.layout_id');
$this->db->join('modules_sections', 'modules_sections.module_id = modules.id');
$this->db->join('sections', 'sections.id = modules_sections.section_id');
$this->db->group_by('modules.id');
$rows = $this->db->get()->result_array();
foreach($rows as &$row)
{
foreach($row as $k=>&$r)
{
if($k=='layout' || $k=='sections')
{
$new_r = explode(DATA_DELIMITER, $r);
foreach($new_r as &$c)
{
$e = explode(DATA_SUBDELIMITER,$c);
$c = array();
list($c['id'],$c['title']) = $e;
if(!empty($e[2])) $c['file'] = $e[2];
}
if($k=='layout') $new_r = $new_r[0];
$r = $new_r;
}
}
}
return $rows;
}
此示例使用 DATA_DELIMITER 和 DATA_SUBDELIMITER 作为分隔数据的字符。如果您使用这些常量,那么您可能应该在您的 application/config/constants.php 文件中定义它们。或许是这样的:
define('DATA_DELIMITER','||');
define('DATA_SUBDELIMITER','##');
测试此代码以产生如下结果:
Array
(
[0] => Array
(
[id] => 1
[title] => Module 1
[layout_id] => 1
[layout] => Array
(
[title] => 3 Column
[id] => 1
[file] => 3_column.php
)
[sections] => Array
(
[0] => Array
(
[title] => Section 1
[id] => 1
)
[1] => Array
(
[title] => Section 2
[id] => 2
)
)
)
[1] => Array
(
[id] => 2
[title] => Module 2
[layout_id] => 2
[layout] => Array
(
[title] => 2 Column
[id] => 2
[file] => 2_column.php
)
[sections] => Array
(
[0] => Array
(
[title] => Section 1
[id] => 1
)
[1] => Array
(
[title] => Section 3
[id] => 3
)
)
)
[2] => Array
(
[id] => 3
[title] => Module 3
[layout_id] => 1
[layout] => Array
(
[title] => 3 Column
[id] => 1
[file] => 3_column.php
)
[sections] => Array
(
[0] => Array
(
[title] => Section 3
[id] => 3
)
)
)
[3] => Array
(
[id] => 4
[title] => Module 4
[layout_id] => 2
[layout] => Array
(
[title] => 2 Column
[id] => 2
[file] => 2_column.php
)
[sections] => Array
(
[0] => Array
(
[title] => Section 1
[id] => 1
)
[1] => Array
(
[title] => Section 2
[id] => 2
)
[2] => Array
(
[title] => Section 3
[id] => 3
)
[3] => Array
(
[title] => Section 4
[id] => 4
)
)
)
)