[英]Fixing Codeigniter Active Record update query
這是在Codeigniter應用程序中處理模型的類
class MY_Model extends CI_Model {
const DB_TABLE = 'abstract';
const DB_TABLE_PK = 'abstract';
private function update() {
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
}
public function save() {
if (isset($this->{$this::DB_TABLE_PK})) {
$this->update();
}
else {
$this->insert();
}}
這是從上一類擴展的模型:
class Projects extends MY_Model {
const DB_TABLE = 'projects';
const DB_TABLE_PK = 'project_id';
public $project_id;
public $project_name;
public $project_investment;
public $project_employment;
public $project_province;
public $project_city;
public $project_address;
public $project_estimate;
public $project_duration;
public $project_construction;
}
根據Codeigniter用戶指南,我認為Update查詢的第三個參數存在問題(它僅發送DB_TABLE_PK名稱,在這種情況下為“ project_id”),但是由於我是OOP的新手,所以不知道如何解決它。
Codeigniter用戶指南:
$this->db->update('mytable', $data, "id = 4");
$data_array = array();
$where_array = array();
$this->db->update('table_name', $where_array, $data_array());
這樣的例子
$data_array = array('name' => 'Alpha', 'gender' => 'male'); // change column name to "Alpha", and gender column to 'male'
$where_array = array('id' => '4', 'allowed' => '1'); // update row with $data_array where id = 4 and allowed = 1
$this->db->update('person', $where_array, $data_array());
我強烈建議您使用探查器$this->output->enable_profiler(TRUE);
將其放置在控制器中的任何位置,頁面下方將顯示“探查器”,該探查器顯示在開發過程中發布/獲取數據的查詢,會話以及所有有用的信息。
上面的函數似乎沒有針對update()的完整WHERE語句
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
將翻譯成
UPDATE $this::DB_TABLE SET $this WHERE $this::DB_TABLE_PK
在上面的示例中,$ this :: DB_TABLE_PK是鍵的名稱('project_id'),而不是其值。
我會堅持
$this->db->where($this::DB_TABLE_PK, $pk)->update($this::DB_TABLE, $this);
其中$ pk是您的主鍵的實際值
我修好了它 :
private function update() {
$value=$this::DB_TABLE_PK;
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK.' = '.$this->$value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.