我正在使用当前版本的 CodeIgniter (2.1.3)。我有三个 MySQL 表:
MySQL 表
操作员
id name
========
1 ACME
标签
id tag
=======
1 bar
2 foo
operator_tag
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),我打开网址http://example.com/tag/remove/2(它成功调用控制器“tag”-> 函数使用参数“2”“删除”。但是大多数时候我收到以下错误
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 查询之前执行的。(我尝试在(第二个)函数中添加一个 INSERT 查询,并在(第一个)remove_tag_from_operator
函数中回显该表的所有结果,该get_operator_for_tag_id
函数神秘地将行(先前生成的)包含在删除函数中。
CodeIgniter 是并行执行查询,还是以任何特殊顺序查询它们?如果是这样,是否有可能禁用此功能?提前致谢!
编辑1:回声值@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));
它工作的行(将我重定向到操作员/编辑/$id)。