CodeIgniterでテーブル結合させるサンプルコード

PHP
スポンサーリンク
//スタッフ情報取得 
$sql = "select a.KANJINAME,b.WARDNAME,c.PROFESSIONNAME "; 
$sql = $sql." from M_STAFF as a "; 
$sql = $sql." LEFT JOIN M_COMWARD as b ON a.STAFFCODE = b.WARDCODE "; 
$sql = $sql." LEFT JOIN M_COMPROFESSION as c ON a.PROFESSIONCODE = c.PROFESSIONCODE "; 
$sql = $sql." WHERE STAFFCODE='".$id."'"; 
$query = $this->db->query($sql); 
if ($query->num_rows() > 0){ 
  $row = $query->row(); 
  $kanjiname=$row->KANJINAME; 
  $wardname=$row->WARDNAME; 
  $professionname=$row->PROFESSIONNAME; 
} 
//スタッフ情報取得 
$data['initVal'] = array($id,$p_id,$kanjiname,$wardname,$professionname);

コントローラ&モデルの場合(ActiveRecord機能)

モデル側

//一覧取得 
function getALL(){ 
  $this->db->select("*"); 
  $this->db->from("T_KIHON"); 
  $this->db->join("m_kubun","T_KIHON.kubun_cd=m_kubun.kubun_cd","left"); 
  $this->db->join("m_syubetsu","T_KIHON.syubetsu_cd=m_syubetsu.syubetsu_cd","left"); 
  $this->db->join("m_staff","T_KIHON.dr_cd=m_staff.staffcode","left"); 
  $this->db->join("m_comward","T_KIHON.ward_cd=m_comward.wardcode","left");  
  $this->db->join("m_comdepartment","T_KIHON.dept_cd=m_comdepartment.deptcode","left"); 
  return $this->db->get(); 
}

コントローラ側

//一覧取得 
$data['rsList'] = $this->m_conference->getAll();
CodeIgniter徹底入門

CodeIgniter徹底入門

タイトルとURLをコピーしました