下面是我的代码,我从三个模型调用三个方法来检索计数如下。
$this->load->model('orders_model');
$order_count = $this->orders_model->count_orders(array("executive_id" => $this->id));
$this->load->model('activities_model');
$activity_count = $this->activities_model->count_activities(array("users_id" => $this->id));
$this->load->model('leads_model');
$leads_count = $this->leads_model->count_leads(array("users_id" => $this->id));
这是我得到的查询:
SELECT COUNT(*)AS
numrows
FROMorders
,activities
,leads
WHEREexecutive_id
='5'和users_id
=' 5'和users_id
='5'
导致数据库错误
为什么会这样?
Orders_model
class Orders_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_order($order_update_data = FALSE, $order_update_condition = FALSE) {
if (!($order_update_data === FALSE && $order_update_condition === FALSE)) {
if (is_array($order_update_data) && is_array($order_update_condition)) {
return $this->db->update('orders', $order_update_data, $order_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_orders($order_custom_condition = FALSE) {
if (!$order_custom_condition === FALSE) {
if (is_array($order_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($order_custom_condition);
$query = $this->db->get('orders');
return $query->result();
}
}
}
public function get_last_ref_id() {
$query = $this->db->query('select sprx_ref_id from orders where id in (select max(id) from orders)');
foreach ($query->result() as $row) {
return $row->sprx_ref_id;
}
}
public function fetch_orders($limit, $start, $order_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($order_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_orders($order_custom_condition) {
$this->db->where($order_custom_condition);
return $this->db->count_all_results('orders', FALSE);
}
}
Activities_model
class Activities_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_activities($activity_id = FALSE) {
if ($activity_id === FALSE) {
$query = $this->db->get('activities');
return $query->result();
}
$this->db->where('id', $activity_id);
#$this->db->order_by('id','ASC');
$query = $this->db->get('activities');
return $query->result();
}
public function add_activity($activity_data = FALSE) {
if (!$activity_data === FALSE) {
if (is_array($activity_data)) {
return $this->db->insert('activities', $activity_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_activity($activity_update_data = FALSE, $activity_update_condition = FALSE) {
if (!($activity_update_data === FALSE && $activity_update_condition)) {
if (is_array($activity_update_data) && is_array($activity_update_condition)) {
return $this->db->update('activities', $activity_update_data, $activity_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_activities($activity_custom_condition = FALSE) {
if (!$activity_custom_condition === FALSE) {
if (is_array($activity_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($activity_custom_condition);
$query = $this->db->get('activities');
return $query->result();
}
}
}
public function fetch_activities($limit, $start, $custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_activities($custom_condition) {
$this->db->where($custom_condition);
return $this->db->count_all_results('activities', FALSE);
}
}
Leads_model
class Leads_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_leads($lead_id = FALSE) {
if ($lead_id === FALSE) {
$query = $this->db->get('leads');
return $query->result();
}
$this->db->where('id', $lead_id);
$query = $this->db->get('leads');
return $query->result();
}
public function add_lead($lead_data = FALSE) {
if (!$lead_data === FALSE) {
if (is_array($lead_data)) {
return $this->db->insert('leads', $lead_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_lead($lead_update_data = FALSE, $lead_update_condition = FALSE) {
if (!($lead_update_data === FALSE && $lead_update_condition)) {
if (is_array($lead_update_data) && is_array($lead_update_condition)) {
return $this->db->update('leads', $lead_update_data, $lead_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_leads($lead_custom_condition = FALSE) {
if (!$lead_custom_condition === FALSE) {
if (is_array($lead_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($lead_custom_condition);
$query = $this->db->get('leads');
return $query->result();
} else {
return false;
}
} else {
return false;
}
}
public function fetch_leads($limit, $start, $lead_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($lead_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads', FALSE);
}
}
答案 0 :(得分:0)
据我所知,您会惊讶于查询构建器使用上一个查询中的其他参数。
您需要根据docs
重置您的查询
这意味着您的所有“count_”函数都应该像
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads');
}
显然你确实故意设置了假旗 - 但我不确定为什么;)
答案 1 :(得分:0)
我认为错误信息只是一个症状,而不是问题的实际原因
。我读代码的方式是3个模型中每个模型中的count_*()
方法只能从各自的表中返回计数。
但是,编写计数函数的方式会导致查询构建器将表和条件添加到整个查询中,而不是仅在单个表上执行它们
$this->db->where($custom_condition); <-- this adds a new where condition using "and" operator
return $this->db->count_all_results('activities', FALSE); <-- just adds another table without resetting the others
我会在3个$this->db->reset_query();
方法的每个方法中添加count_*()
行作为第1行,以强制查询构建器从头开始。
答案 2 :(得分:0)
问题是由于使用$this->db->count_all_results()
的第二个参数。当您将第二个参数设置为FALSE时,$this->db
将不会清除其缓存中的任何select
语句。然后,对count_all_results()
的每次连续调用都将包括来自该函数的任何先前调用的表。解决方案很简单 - 不要使用第二个参数。
更改
return $this->db->count_all_results('activities', FALSE);
到
return $this->db->count_all_results('activities');
与您的问题无关,但改善代码的方法正在改变这个
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
到
public function get_orders($order_id = NULL) {
if (!empty($order_id))
{
$this->db->where('id', $order_id);
}
$query = $this->db->get('orders');
return $query->result();
}
将参数default更改为NULL并使用!empty($order_id)
会有所帮助,因为它可以防止和作为参数提供空字符串或空数组。 (准备好empty()
here。)
此新逻辑还会将代码保留为DRY - 您不会将两行代码重复为get
和return
结果。
您的许多其他模型功能也可能更清晰。例如
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
会像这样写得更干净
public function add_order($order_data = NULL) {
if (!empty($order_data) && is_array($order_data))
{
return $this->db->insert('orders', $order_data);
}
return false;
}
很抱歉挑剔你的代码 - 我无法自拔。