簡體   English   中英

即使指定了列名,Ignited Datatables 也會選擇所有列

[英]Ignited Datatables selecting all columns even when column names are specified

我似乎無法讓這個工作,它讓我發瘋。 我正在使用我在github 上找到的 CodeIgniter 的 Ignited Datatables 庫

我有以下方法可以從連接到另一個表中獲取記錄:

public function get_templates_ajax() {
        $this->datatables->select('tem.template_id, tem.name, tem.vat, COUNT(itm.template_id) AS total_items');
        $this->datatables->from('templates tem');
        $this->datatables->join('items itm', 'tem.template_id = itm.template_id', 'left');
        $this->datatables->where('tem.user_id', $this->session->user_id);
        $this->datatables->group_by('tem.template_id');
        $this->datatables->add_column('actions', '<a href="javascript:void(0);" class="edit_record btn btn-info" data-id="$1" data-name="$2" data-vat="$3">Edit</a><a href="javascript:void(0);" class="delete_record btn btn-danger" data-id="$1">Delete</a>', 'tem.template_id, tem.name, tem.vat');
        return $this->datatables->generate();
} 

是的,我在兩個表中都有一個名為user_id的字段,但我沒有選擇該列,但是,我收到此錯誤:重復列名“user_id”。

我指定了我想要選擇的列,如下所示:

$this->datatables->select('tem.template_id, tem.name, tem.vat, COUNT(itm.template_id) AS total_items');

我知道如果我想選擇 user_id 列,我會使用別名,但我沒有選擇它。

$this->db->last_query(); 轉儲此 SQL:

SELECT COUNT(*) FROM (SELECT * FROM `templates` `tem` LEFT JOIN `items` `itm` ON `tem`.`template_id` = `itm`.`template_id` WHERE `tem`.`user_id` = '1' GROUP BY `tem`.`template_id`) SqueryAux

因此,在從兩個表中選擇所有列時,似乎忽略了 select 方法。 我可能做錯了什么?

經過多次挖掘,我通過對庫進行了一些調整來解決問題。

顯然,問題源於get_total_results(); 類中的方法,更具體地說是這兩行:

$subquery = $this->ci->db->get_compiled_select($this->table);
$countingsql = "SELECT COUNT(*) FROM (" . $subquery . ") SqueryAux";

子查詢變量從主表中選擇所有列,並使用老舊*連接表。

這是我為解決問題所做的工作:

我用這個替換了$subquery行:

....
//ensure table with alias does not contain more than 1 space between original table name and alias
$this->table = preg_replace('!\s+!', ' ', $this->table);
$tbl = explode(" ", $this->table);
//if table has alias, get it, else get table name
$table = count($tbl) > 1 ? $tbl[1] : $tbl[0];
$subquery = $this->ci->db->get_compiled_select($this->table);
//prefix table/alias to * so it selects columns from the main table only
$subquery = str_replace("*", "{$table}.*", $subquery);
....

希望它可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM