我跟着表。
apartments
id name slug created modified
apartment_amenities
id name slug apartment_id created modified
apartment_activities
id name slug apartment_id created modified
在视图中我想要这样的东西。
no apartment_name amenities activities
1 shobha_comnplex party hall pamplets
swimming pool banners
play area boards
2 navami_comnplex party hall boards
swimming pool banners
club house pamplets
在模特中我试过这样的事。
$this->db->select('apartments.id, apartments.slug, apartments.name, apartment_amenities.name as amenity_name, apartment_activities.name as activity_name');
$this->db->from($this->_table);
$this->db->join('apartment_amenities', 'apartment_amenities.apartment_id = apartments.id', 'left');
$this->db->join('apartment_activities', 'apartment_activities.apartment_id = apartments.id', 'left');
return $this->db->get();
但即使公寓有许多设施和活动,我只能获得单一的设施和活动。结果如下。
Array
(
[0] =>
(
[id] => 1
[slug] => shobha_complex
[name] => shobha complex
[amenity_name] => party hall
[activity_name] => pamplets
),
[1] =>
(
[id] => 1
[slug] => navami_complex
[name] => navami complex
[amenity_name] => party hall
[activity_name] => boards
)
)
我希望结果如下所示。
Array
(
[0] =>
(
[id] => 1
[slug] => shobha_complex
[name] => shobha complex
[amenities] => Array(
[0] =>
(
[name] => party hall
),
[1] =>
(
[name] => swimming pool
),
[2] =>
(
[name] => play area
)
),
[activities] => Array(
[0] =>
(
[name] => pamplets
),
[1] =>
(
[name] => banners
),
[2] =>
(
[name] => boards
)
)
),
[1] =>
(
[id] => 1
[slug] => navami_complex
[name] => Navami complex
[amenities] => Array(
[0] =>
(
[name] => party hall
),
[1] =>
(
[name] => swimming pool
),
[2] =>
(
[name] => club house
)
),
[activities] => Array(
[0] =>
(
[name] => boards
),
[1] =>
(
[name] => banners
),
[2] =>
(
[name] => pamplets
)
)
),
)
请建议我如何获得解决方案。这项工作将更受赞赏。
答案 0 :(得分:1)
您可以在select上使用group concat并将其用作分隔符|因此,您将只获得两行,并在视图中使用分隔符
拆分列$this->db->select("GROUP_CONCAT(partment_amenities.name SEPARATOR '|') as amenity_name, ...... ", FALSE);
第二个参数FALSE表示不保护标识符 有关组concate的更多信息,请参阅链接: https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
答案 1 :(得分:0)
删除$this->db->group_by
方法调用。
这只会为每个公寓返回一个结果。如果你删除它,你应该得到你想要的所有设施等。
唯一的问题是你得到这样的结果
no apartment_name amenities activities
1 shobha_comnplex party hall pamplets
1 shobha_comnplex swimming pool banners
1 shobha_comnplex play area boards
2 navami_comnplex party hall boards
2 navami_comnplex swimming pool banners
2 navami_comnplex club house pamplets
答案 2 :(得分:0)
要设置一个合适的数组,我们会以你想要的格式建议你有一些模型。
让我们设置一个场景
<强>控制器:强>
$apartments = $this->apartment_model->getApartments(); // Get All Apartments
foreach($apartments as &$apartment)
{
$apartment->amenities = $this->apartment_model->getAmenities($apartment->id);
$apartment->activities= $this->apartment_model->getActivities($apartment->id);
// Add / Modify any addition properties
}
// Do something with the $apartments array
<强>型号:强>
class Apartment_model extends model
{
function getApartments()
{
$this->db->select('id, slug, name');
$this->db->from('apartments');
$query = $this->db->get();
return $query->result();
}
function getAmenities($apartment_id)
{
$this->db->select('name');
$this->db->from('apartment_amenities');
$this->db->where('id', $apartment_id);
$query = $this->db->get();
return $query->result();
}
function getActivities($apartment_id)
{
$this->db->select('name');
$this->db->from('apartment_activities');
$this->db->where('id', $apartment_id);
$query = $this->db->get();
return $query->result();
}
}
当然要记住,这还没有加载视图或者将数据移动到控制器的任何地方,但它至少会设置数组。
我希望这有助于您入门。