如何在Codeigniter中的同一个表中将1个表与2个外键连接起来。我尝试使用SQL进行查询,但它在Codeigniter中与DataTable集成时却无法工作。以下codeigniter SQL代码位于下方。
查询必须是这样的,这项工作做得很好:
SELECT * FROM kf_emails
JOIN kf_emailserver AS A
ON kf_emails.kf_email_incomingserver = A.kf_emailserver_id
JOIN kf_emailserver AS B
ON kf_emails.kf_email_incomingserver = B.kf_emailserver_id
ORDER BY kf_email_id DESC;
这是在codeigniter
view.php
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>"/>
<link rel="stylesheet" href="<?php echo base_url('assets/datatables/dataTables.bootstrap.css') ?>"/>
<link rel="stylesheet" href="<?php echo base_url('assets/datatables/dataTables.bootstrap.css') ?>"/>
<style>
.dataTables_wrapper {
min-height: 500px
}
.dataTables_processing {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
margin-left: -50%;
margin-top: -25px;
padding-top: 20px;
text-align: center;
font-size: 1.2em;
color:grey;
}
body{
padding: 15px;
}
</style>
</head>
<body>
<div class="row" style="margin-bottom: 10px">
<div class="col-md-4">
<h2 style="margin-top:0px">Kf_emails List</h2>
</div>
<div class="col-md-4 text-center">
<div style="margin-top: 4px" id="message">
<?php echo $this->session->userdata('message') <> '' ? $this->session->userdata('message') : ''; ?>
</div>
</div>
<div class="col-md-4 text-right">
<?php echo anchor(site_url('kf_emails/create'), 'Create', 'class="btn btn-primary"'); ?>
<?php echo anchor(site_url('kf_emails/excel'), 'Excel', 'class="btn btn-primary"'); ?>
</div>
</div>
<table class="table table-bordered table-striped" id="mytable">
<thead>
<tr>
<th width="80px">No</th>
<th>Kf Email Emailaddress</th>
<th>Kf Email Incomingserver</th>
<th>Kf Email Outgoingserver</th>
<th>Kf Email Emailusername</th>
<th>Kf Email Emailpassword</th>
<th>Kf Email Status</th>
<th width="200px">Action</th>
</tr>
</thead>
</table>
<script src="<?php echo base_url('assets/js/jquery-1.11.2.min.js') ?>"></script>
<script src="<?php echo base_url('assets/datatables/jquery.dataTables.js') ?>"></script>
<script src="<?php echo base_url('assets/datatables/dataTables.bootstrap.js') ?>"></script>
<script type="text/javascript">
$(document).ready(function() {
$.fn.dataTableExt.oApi.fnPagingInfo = function(oSettings)
{
return {
"iStart": oSettings._iDisplayStart,
"iEnd": oSettings.fnDisplayEnd(),
"iLength": oSettings._iDisplayLength,
"iTotal": oSettings.fnRecordsTotal(),
"iFilteredTotal": oSettings.fnRecordsDisplay(),
"iPage": Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
"iTotalPages": Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
};
};
var t = $("#mytable").dataTable({
initComplete: function() {
var api = this.api();
$('#mytable_filter input')
.off('.DT')
.on('keyup.DT', function(e) {
if (e.keyCode == 13) {
api.search(this.value).draw();
}
});
},
oLanguage: {
sProcessing: "loading..."
},
processing: true,
serverSide: true,
ajax: {"url": "kf_emails/json", "type": "POST"},
columns: [
{
"data": "kf_email_id",
"orderable": false
},{"data": "kf_email_emailaddress"},{"data": "kf_emailserver_serveraddress"},{"data": "kf_emailserver_serveraddress"},{"data": "kf_email_emailusername"},{"data": "kf_email_emailpassword"},{"data": "kf_email_status"},
{
"data" : "action",
"orderable": false,
"className" : "text-center"
}
],
order: [[0, 'desc']],
rowCallback: function(row, data, iDisplayIndex) {
var info = this.fnPagingInfo();
var page = info.iPage;
var length = info.iLength;
var index = page * length + (iDisplayIndex + 1);
$('td:eq(0)', row).html(index);
}
});
});
</script>
</body>
</html>
模型
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Kf_emails_model extends CI_Model
{
public $table = 'kf_emails';
public $id = 'kf_email_id';
public $order = 'DESC';
public $table1 = 'kf_emailserver';
public $id1 = 'kf_emailserver_id';
public $order1 = 'DESC';
function __construct()
{
parent::__construct();
}
// datatables
function json() {
$this->datatables->select('kf_email_id,kf_email_emailaddress,kf_emailserver_serveraddress,kf_emailserver_serveraddress,kf_email_emailusername,kf_email_emailpassword,kf_email_status');
$this->datatables->from('kf_emails');
//add this line for join
$this->datatables->join('kf_emailserver AS A', 'kf_emails.kf_email_incomingserver = kf_emailserver.kf_emailserver_id');
$this->datatables->join('kf_emailserver AS B', 'kf_emails.kf_email_outgoingserver = kf_emailserver.kf_emailserver_id');
//$this->datatables->join('table2', 'kf_emails.field = table2.field');
$this->datatables->add_column('action', anchor(site_url('kf_emails/read/$1'),'Read')." | ".anchor(site_url('kf_emails/update/$1'),'Update')." | ".anchor(site_url('kf_emails/delete/$1'),'Delete','onclick="javasciprt: return confirm(\'Are You Sure ?\')"'), 'kf_email_id');
return $this->datatables->generate();
}
// get all
function get_all()
{
$this->db->order_by($this->id, $this->order);
return $this->db->get($this->table)->result();
}
// get data by id
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
// get total rows
function total_rows($q = NULL) {
$this->db->like('kf_email_id', $q);
$this->db->or_like('kf_email_emailaddress', $q);
$this->db->or_like('kf_email_incomingserver', $q);
$this->db->or_like('kf_email_outgoingserver', $q);
$this->db->or_like('kf_email_emailusername', $q);
$this->db->or_like('kf_email_emailpassword', $q);
$this->db->or_like('kf_email_status', $q);
$this->db->from($this->table);
return $this->db->count_all_results();
}
// get data with limit and search
function get_limit_data($limit, $start = 0, $q = NULL) {
$this->db->order_by($this->id, $this->order);
$this->db->like('kf_email_id', $q);
$this->db->or_like('kf_email_emailaddress', $q);
$this->db->or_like('kf_email_incomingserver', $q);
$this->db->or_like('kf_email_outgoingserver', $q);
$this->db->or_like('kf_email_emailusername', $q);
$this->db->or_like('kf_email_emailpassword', $q);
$this->db->or_like('kf_email_status', $q);
$this->db->limit($limit, $start);
return $this->db->get($this->table)->result();
}
// insert data
function insert($data)
{
$this->db->insert($this->table, $data);
}
// update data
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
// delete data
function delete($id)
{
$this->db->where($this->id, $id);
$this->db->delete($this->table);
}
}
答案 0 :(得分:1)
你可以试试这个:
<强>控制器强>
public function index() {
$join_str[0] = array('table' => 'category',
'join_table_id' => 'category.category_id',
'from_table_id' => 'product.category_id',
"join_type" => 'left'
);
$condition_array = array();
$data = 'product.*,category.category_name';
$result_product_details = $this->common->select_data_by_condition('product', $condition_array, $data, $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str, $groupby = '');
}
<强>模型强>
function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) {
$this->db->select($data);
$this->db->from($tablename);
//if join_str array is not empty then implement the join query
if (!empty($join_str)) {
foreach ($join_str as $join) {
if (!isset($join['join_type'])) {
$this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']);
} else {
$this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']);
}
}
}
//condition array pass to where condition
$this->db->where($condition_array);
//Setting Limit for Paging
if ($limit != '' && $offset == 0) {
$this->db->limit($limit);
} else if ($limit != '' && $offset != 0) {
$this->db->limit($limit, $offset);
}
//order by query
if ($sortby != '' && $orderby != '') {
$this->db->order_by($sortby, $orderby);
}
$query = $this->db->get();
//if limit is empty then returns total count
if ($limit == '') {
$query->num_rows();
}
//if limit is not empty then return result array
log_message('debug', 'fetching data result:' . $this->db->last_query());
return $query->result_array();
}