【发布时间】:2013-01-22 14:02:19
【问题描述】:
我正在使用当前版本的 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),我打开 url http://example.com/tag/remove/2(它成功调用控制器“tag”-> 函数“remove”,参数“ 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 查询之前执行的。 (我尝试在(第二个)remove_tag_from_operator 函数中添加一个 INSERT 查询,并在(第一个)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)); 行,它会起作用(将我重定向到 operator/edit/$id)。
【问题讨论】:
-
你说大部分时间这是否意味着上面的代码有时有效?如果是,您是否注意到它起作用时的任何模式? 2、如果你注释掉上面代码中的
delete查询,它返回的id是不是没有任何错误信息? -
在
operator_tag中,为什么两行都有id和1? -
-itachi 如果我注释掉删除块它总是有效的(将我重定向回操作员/编辑/$id)。是的,该代码有时会起作用。我无法识别模式。我将编辑主要帖子,以发布另一个代码示例。 -RocketHazmat 抱歉,这只是一个错字。当然,第二行的 id 应该是 2(我已经解决了这个问题)。
标签: php mysql database codeigniter activerecord