php - CodeIgniter 以错误的顺序执行数据库查询(可能是并行的?)

标签 php mysql database codeigniter activerecord

我使用的是当前版本的 CodeIgniter (2.1.3)。我有三个 MySQL 表:

MySQL 表

运营商

id  name
========
1   ACME

标签

id  tag
=======
1   bar
2   foo

运算符标签

id  operator_id  tag_id
=======================
1   1            1
2   1            2

因此运算符 ACME 被标记为两个标签(bar 和 foo)。

CodeIgniter 文件

我在尝试删除这样的标签时遇到错误:

//file: controllers/tag.php (function contained in class Tag extends CI_Controller)

//this function should remove the tag with the id $id and redirect back to the edit page for the operator
public function remove($id){
    $operator_id = $this->operator_model->get_operator_for_tag_id($id);
    $this->operator_model->remove_tag_from_operator($id);
    redirect('operator/edit/'.$operator_id);
}

..

//file: models/operator_model.php (functions contained in class Operator_model extends CI_Model)

public function get_operator_for_tag_id($id){
    $query = $this->db->select('operator_id')->from('operator_tag')->where('id',$id)->get();
    return $query->row()->operator_id;
}

public function remove_tag_from_operator($id){
    $this->db->delete('operator_tag',array('id' => $id));
}

错误

如果我调用函数从运算符中删除标签“foo”(id:2),我打开 url http://example.com/tag/remove/2 (使用参数“2”成功调用 Controller “tag”-> 函数“remove”。但是大多数时候我收到以下错误

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/operator_model.php
Line Number: XX (which is line with `return $query->row()->operator_id;`)

看来,DELETE 查询是在 SELECT 查询之前执行的。 (我试图在(第二个)remove_tag_from_operator 函数中添加一个 INSERT 查询,并在(第一个)get_operator_for_tag_id 函数中回显该表的所有结果,该函数神秘地包含了行(之前在删除函数中生成)。

CodeIgniter 是并行执行查询,还是以任何特殊顺序查询它们?如果是这样,是否有可能禁用它?提前致谢!

编辑 1: echo 值@luc

@luc 我将代码更改为以下(添加了回显行)以进行调试:

//file: controllers/tag.php
public function remove($id){
    echo '-1. CONTROLLER '.$id.'<br>';
    $operator_id = $this->operator_model->get_operator_for_tag_id($id);
    echo '-2. CONTROLLER '.$id.'<br>';
    $this->operator_model->remove_tag_from_operator($id);
    echo '-3. CONTROLLER '.$id.'<br>';
    redirect('operator/edit/'.$operator_id);
}
//file: models/operator_model.php
public function get_operator_for_tag_id($id)
{
    echo '-1.1 GET '.$id.'<br>';
    $query = $this->db->select('operator_id')->from('operator_tag')->where('id',$id)->get();
    echo '-1.2 GET '.$id.'<br>';
    $result = $query->row()->operator_id;
    echo '-1.3 GET '.$id.'<br>';
    return $result;
}

public function remove_tag_from_operator($id){
    echo '-2.1 REMOVE '.$id.'<br>';
    $this->db->delete('operator_tag',array('id' => $id));
    echo '-2.2 REMOVE '.$id.'<br>';
}

在调用 http://example.com/tag/remove/41 时输出如下内容

-1. CONTROLLER 41  
-1.1 GET 41  
-1.2 GET 41  

**A PHP Error was encountered**  
Severity: Notice  
Message: Trying to get property of non-object  
Filename: models/operator_model.php
Line Number: 236 (which is the row with `$result = $query->row()->operator_id;`)

-1.3 GET 41  
-2. CONTROLLER 41  
-2.1 REMOVE 41  
-2.2 REMOVE 41  
-3. CONTROLLER 41  

**A PHP Error was encountered**  
Severity: Warning  
Message: Cannot modify header information - headers already sent by (output started at .../application/controllers/tag.php:242)  (which is the output generated by `echo '1. CONTROLLER '.$id.'<br>';`)
Filename: helpers/url_helper.php  
Line Number: 542

因此 $id 被正确传递并且回显的顺序正确。只有数据库查询以神秘的方式执行。

编辑 2:检查行@itachi

@itachi 我更改了以下代码以进行调试(如果找不到值,则输出整个 operator_tag-table)

//file: controllers/tag.php
public function get_operator_for_tag_id($id)
{
    $query = $this->db->select('operator_id')->from('operator_tag')->where('id',$id)->get();
    if ($query->num_rows() > 0){
        return $query->row()->operator_id;
    }else{
        $query = $this->db->get('operator_tag');
        print_r($query->result());
    }
}

在调用 http://example.com/tag/remove/44 时输出如下内容

Array ( [0] => stdClass Object ( [id] => 3 [operator_id] => 40 [tag_id] => 1 ) )
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at .../application/models/operator_model.php:236) (which is the line with `print_r($query->result());`)
Filename: helpers/url_helper.php
Line Number: 542

我试图重现工作示例,但我失败了。如果我注释掉 $this->db->delete('operator_tag',array('id' => $id)); 行它起作用(将我重定向到 operator/edit/$id ).

最佳答案

该错误的发生主要是因为查询字符串不完整。你在某处有 $this->uri->segment(3) 来获取 $id 吗?我在你的代码中没有看到它。 $id 在哪里定义?

尝试呼应它的值(value),我认为这就是问题所在。

关于php - CodeIgniter 以错误的顺序执行数据库查询(可能是并行的?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460347/

相关文章:

php - Laravel 5 删除尾部斜线时的不良行为

mysql - 如何将列名中有空格的 CSV 导入 MySQL?

php - 未使用准备好的语句存储的数据

mysql - 如何评价数据库系统中一张表的好坏?

database - 新表无法识别

PHP 表单, session 数据未存储

php - "\"在 PHP/MySQL 脚本中做什么

php - 没有看到查询结果回显

php - 使用 www 强制使用 HTTPS 并在 CodeIgniter 中删除 index.php .htaccess

php - 为什么 MySQL 不返回最后插入的 id,它返回 0?