Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 修复Codeigniter活动记录更新查询_Php_Codeigniter - Fatal编程技术网

Php 修复Codeigniter活动记录更新查询

Php 修复Codeigniter活动记录更新查询,php,codeigniter,Php,Codeigniter,这是一个在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); }

这是一个在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用户指南,我认为更新查询的第3个参数有问题(它只发送DB_TABLE_PK name,在本例中为'project_id'),但由于我是OOP新手,不知道如何修复它

Codeigniter用户指南:

$this->db->update('mytable', $data, "id = 4");

以身作则

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

我强烈建议您使用profiler
$this->output->enable_profiler(TRUE)将其放在控制器中的任何位置,在您的页面下将显示“profiler”,显示开发时的post/get数据查询、会话和所有有用信息。

上面的函数似乎没有更新()的完整WHERE语句

将转化为

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

}

这正是我的意思。但是在OOP编程中我应该写什么来代替$pk呢?我的问题是如何在OOP方法中写这个id值(4)?你必须使用
array()
structure,或者当你写
$this->db->update('mytable',$data,“id=4”)。在这一点上,我想我不完全理解你的问题。
$this->db->where($this::DB_TABLE_PK, $pk)->update($this::DB_TABLE, $this);
private function update() {
    $value=$this::DB_TABLE_PK;
    $this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK.' = '.$this->$value);

}