在我的控制器上,我想联系另一个模型,将结果添加到现有数组中。
我的控制器:
public function index()
{
$getConcerts_previous = $this->concert_model->getAllConcerts('previous');
foreach($getConcerts_previous as $concert) {
$hallData = $this->hall_model->getHallbyID($concert->cHall_ID);
$concert->hHall_ID = isset($hallData) ? $hallData->hID : 'No data';
}
$this->render_layout('concerts/index', $this->data);
}
当我在我的视图中@varp_dump我的'$ getConcerts_previous'时,我有:
public 'cID' => string '45' (length=2)
public 'cBand_ID' => string '8' (length=1)
public 'cDate' => string '2018-06-22' (length=10)
我想知道如何添加此var转储,例如
public 'cID' => string '45' (length=2)
public 'cBand_ID' => string '8' (length=1)
public 'cDate' => string '2018-06-22' (length=10)
public 'hHall_ID' => string '1' (length=1)
我的数据库:
Concerts: cID | cName | cPlaceType | cPlaceID
Festivals: fID | fName
Halls: hID | hName
如果cPlaceType = 1,请选择带有cPlaceID的festival,如果cPlaceType = 2,则选择带有cPlaceID的hall,
答案 0 :(得分:1)
试试这个
public function index()
{
$this->data['getConcerts_next'] = $this->concert_model->getAllConcerts('next');
$getConcerts_previous = $this->concert_model->getAllConcerts('previous');
foreach($getConcerts_previous as $concert) {
if($concert->cPlaceType == 1) {
$hallData = $this->hall_model->getHallbyID($concert->cPlaceID);
//$concert->hHall_ID = isset($hallData) ? $hallData->hID : 'No data';
$concert->hallData = $hallData;
$concert->festivalData = NULL;
} else {
$festivalData = $this->festival_model->getFestivalbyID($concert->cPlaceID);
//$concert->hHall_ID = isset($festivalData) ? $festivalData->fID : 'No data';
$concert->hallData = NULL;
$concert->festivalData = $festivalData;
}
}
var_dump($getConcerts_previous);
//include below line and you can pass data to view;
$this->data['getConcerts_previous'] = $getConcerts_previous;
$this->render_layout('concerts/index', $this->data);
}
在模型中
public function getHallById($hallID) {
$this->db->where('hID', $hallID);
return $this->db->get($this->table)->row();
}
public function getFestivalById($hallID) {
$this->db->where('fID', $festivalID);
return $this->db->get($this->table)->row();
}