gpt4 book ai didi

php - CodeIgniter:连续两个模型方法导致内部服务器错误

转载 作者:可可西里 更新时间:2023-10-31 23:38:31 28 4
gpt4 key购买 nike

我是 CodeIgniter 的新手,正在尝试完成我的第一个项目。我有两个 ajax db-update 函数,它们(对它们自己)工作得很好。

(因为这样的查询有效,我为了这个例子简化了:)

    public function updateA($data) {

$this->pos1 = $data['pos1'];
$this->pos2 = $data['pos2'];

$this->db->where('id', 1);

$result = $this->db->update('tablePos', $this);
}

    public function updateB($data) {

foreach ($data as $value) {

$this->name = $value['name'];
$this->type = $value['type'];
$this->db->where('id', $value['ID']);

$result = $this->db->update('tableNames', $this);
}

}

如前所述,如果我在我的 Controller 中“单独”调用它们,它们都可以正常工作。好喜欢

$this->MainModel->updateA($data);

$this->MainModel->updateB($data);

但不是

$this->MainModel->updateA($data);
$this->MainModel->updateB($data);

然后执行第一个查询,但不执行第二个。我先调用哪个都没有关系。如果我连续调用这两个函数,则只执行拳头并且我的 ajax 函数返回一个

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

我整晚都在寻找 - 毫 headless 绪...... :(

编辑:

我现在将所有内容隔离到一个测试用例中:

Controller :

        public function parse()
{
$this->load->model('MainModel');

$position = '{"ID":1,"pos1":5,"pos2":6}';
$position = json_decode($position, true);

$names = '[{"ID":1,"name":"AAAA","type":9},{"ID":2,"name":"BBBBB","type":2},{"ID":3,"name":"CC","type":4}]';
$names = json_decode($names, true);

$this->MainModel->updateA($position);
$this->MainModel->updateB($names);
}

型号:

        public function updateA($data_a) {
error_log("----------- start A -----------");
$this->pos1 = $data_a['pos1'];
$this->pos2 = $data_a['pos2'];
$this->db->where('id', 1);
$result = $this->db->update('tablePos', $this);
error_log($this->db->last_query());
error_log("----------- stop A -----------");
}


public function updateB($data_b) {
error_log("----------- start B -----------");
foreach ($data_b as $value) {

$this->name = $value['name'];
$this->type = $value['type'];
$this->db->where('id', $value['ID']);

$result = $this->db->update('tableNames', $this);
error_log($this->db->last_query());
}
error_log("----------- stop B -----------");
}

两次更新调用的错误日志结果:

[03-Dec-2015 14:13:15 Europe/Berlin] ----------- start A -----------

[03-Dec-2015 14:13:15 Europe/Berlin] UPDATE tablePos SET pos1 = 5, pos2 = 6 WHERE id = 1

[03-Dec-2015 14:13:15 Europe/Berlin] ----------- stop A -----------

[03-Dec-2015 14:13:15 Europe/Berlin] ----------- start B -----------

结果

    $this->MainModel->updateA($position);
// $this->MainModel->updateB($names);

[03-Dec-2015 14:23:23 Europe/Berlin] ----------- start A -----------

[03-Dec-2015 14:23:23 Europe/Berlin] UPDATE tablePos SET pos1 = 5, pos2 = 6 WHERE id = 1

[03-Dec-2015 14:23:23 Europe/Berlin] ----------- stop A -----------

结果

    // $this->MainModel->updateA($position);
$this->MainModel->updateB($names);

[03-Dec-2015 14:25:14 Europe/Berlin] ----------- start B -----------

[03-Dec-2015 14:25:14 Europe/Berlin] UPDATE tableNames SET name = 'AAAA', type = 9 WHERE id = 1 [03-Dec-2015 14:25:14 Europe/Berlin]

UPDATE tableNames SET name = 'BBBBB', type = 2 WHERE id = 2

[03-Dec-2015 14:25:14 Europe/Berlin] UPDATE tableNames SET name = 'CC', type = 4 WHERE id = 3

[03-Dec-2015 14:25:14 Europe/Berlin] ----------- stop B -----------

它只是停止了。没有错误。当我挑起它们时,错误日志确实会显示其他 php 错误。所以这对我来说绝对是神秘的......

最佳答案

尝试改变你的方法来使用局部变量而不是类属性 $this 像这样:

public function updateA($data_a) {
error_log("----------- start A -----------");
$this->db->where('id', 1);
$updateData = array(
'pos1' => $data_a['pos1'],
'pos2' => $data_a['pos2'],
);
$result = $this->db->update('tablePos', $updateData);
error_log($this->db->last_query());
error_log("----------- stop A -----------");
}

  public function updateB($data_b) {
error_log("----------- start B -----------");
foreach ($data_b as $value) {
$updateData = array(
'name' => $value['name'],
'type' => $value['type'],
);
$this->db->where('id', $value['ID']);

$result = $this->db->update('tableNames',$updateData);
error_log($this->db->last_query());
}
error_log("----------- stop B -----------");
}

一个可能的原因可能是因为加载模型时初始化了类属性。

当您使用 $this 进行更新时,对于第二次调用,该类仍然保留在第一次更新期间使用的属性,并且由于此处不需要它们,因此它们会导致错误。

但是我不确定这些值如何没有反射(reflect)在您记录的查询中。

关于php - CodeIgniter:连续两个模型方法导致内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34065772/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com