2016-09-13 5 views
1

Unten ist mein Code, wo ich drei Methoden von drei Modellen aufrufen, um die Anzahl wie folgt abzurufen.Seltsame Abfrage generiert in Codeigniter

$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)); 

Und das ist die Abfrage erhalte ich:

SELECT COUNT (*) AS numrows VON orders, activities, leads WHERE executive_id = '5' und users_id = '5' und users_id = '5'

, die zu einem Datenbankfehler führt

screenshot of error

Warum passiert das?

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

den Modellcode zeigen –

+0

Ok warten nur eine Minute –

+0

Wahrscheinlich haben Sie 'users_id' in mehr als einer Tabelle (Aufträge, Aktivitäten, führt) – krlzlx

Antwort

0

Soweit ich verstehe, sind Sie überrascht, warum die Query Builder zusätzliche Parameter aus der vorherigen Abfrage verwendet.

Sie haben Ihre Abfrage nach docs

zurückgesetzt, welche wie

public function count_leads($lead_custom_condition) { 
    $this->db->where($lead_custom_condition); 
    return $this->db->count_all_results('leads'); 

} 
sein sollten alle Ihre „count_“ -Funktionen bedeutet

Sie offensichtlich die falsche Flagge absichtlich gesetzt haben - aber ich bin mir nicht sicher warum;)

+0

es funktioniert, danke –

0

Ich glaube, dass die Fehlermeldung nur ein Symptom ist, nicht die eigentliche Ursache des Problems

. Die Art, wie ich Ihren Code lese, ist, dass die count_*()-Methoden in jedem der 3 Modelle die Anzahl nur aus ihren jeweiligen Tabellen zurückgeben sollten.

jedoch die Art und Weise schreiben Sie Ihre Zählfunktionen Ergebnisse im Query Builder die Tabellen und Bedingungen für die Gesamt Abfrage hinzufügen, sie nicht nur auf den einzelnen Tabellen Ausführung

$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 

Ich mag hinzufügen, ein $this->db->reset_query(); Linie als die erste Zeile in jeder der 3 count_*()-Methoden, um den Abfrage-Generator von Grund auf neu zu erzwingen.

+0

Problem gelöst durch Entfernen der FALSE-Flag in count_all_results(); , –

0

Das Problem liegt an der Verwendung des zweiten Arguments zu $this->db->count_all_results(). Wenn Sie das zweite Argument auf FALSE festlegen, löscht $this->db keine select-Anweisungen aus seinem Cache. Dann wird jeder nachfolgende Anruf an count_all_results() die Tabelle von jedem vorherigen Aufruf der Funktion enthalten. Die Lösung ist einfach - verwende nicht den zweiten Parameter.

ändern

return $this->db->count_all_results('activities', FALSE); 

zu

return $this->db->count_all_results('activities'); 

nicht zu Ihrem Problem zu tun haben, sondern etwas, das Ihren Code verbessern ändert diese

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(); 
} 

zu

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(); 
} 

Das Ändern des Argumentstandards auf NULL und das Verwenden von !empty($order_id) hilft, da es vor einem leeren String oder einem leeren Array schützt, das als Argument angegeben wird. (. Klar zum Wenden empty()here)

Diese neue Logik hält auch den Code DRY - Sie nicht die zwei Codezeilen get und return Ergebnisse wiederholen.

Viele Ihrer anderen Modellfunktionen könnten auch sauberer sein. Zum Beispiel

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; 
    } 
} 

Reiniger würde wie diese für Erbsenzählerei Code

public function add_order($order_data = NULL) { 
    if (!empty($order_data) && is_array($order_data)) 
    { 
     return $this->db->insert('orders', $order_data); 
    } 
    return false; 
} 

Leider geschrieben - ich mich nicht helfen konnte.