2013-10-23 34 views
1

我有一个自定义的CodeIgniter库,我需要运行一些数据库操作。CodeIgniter:自定义库的insert_batch - 失败,无返回或错误 - 如何调试?

要访问数据库,我执行此命令,我在这里学到在计算器上:

$this->CI =& get_instance(); 

我知道它的工作,因为这个命令的工作:

$drop_query = "DELETE FROM product_images WHERE product_id = " . $this->CI->db->escape($product_id); 
$this->CI->db->query($drop_query); // Tested, this works. 

然而,insert_batch完全不起作用。没有错误返回,没有错误...没什么。它刚刚死亡。

$master_insert_array = array(
     array(
      'product_id' => $product_id, 
      'thumb_type' => 'original', 
      'filename' => $orig_file_name 
     ), 
     array(
      'product_id' => $product_id, 
      'thumb_type' => 'small', 
      'filename' => $small_file_name 
     ) // Truncating the rest of this... 
    ); 

    error_log("update_product_image_db: Insert_Batch Coming up:"); 

    $batch_success = $this->CI->db->insert_batch('product_images', $master_insert_array); 
    error_log("\n\n".$this->CI->db->last_query()."\n"); 

*编辑:原来,我的$的product_id是失败的外键约束,使得batch_insert失败*

第二error_log中永远不会被失败后运行。剧本刚刚死亡。

那么我怎样才能得到insert_batch正确地返回我一个错误,一个错误,或者其他东西比扁平失败失败?

*更新:我也尝试把它放到try/catch块中,但没有成功。如果它失败了外键约束,它会中止我的整个脚本。这是我目前的意见,insert_batch是一个写得不好的功能*

谢谢!

+0

使用'回声$这个 - > DB-> last_query()'刚刚过去'error_log'之前,看到的输出。 –

+0

这没有奏效,但事实证明,我的$ product_id WAS失败的外键约束。所以这是一个流言蜚语。对于那个很抱歉。 第二次更新这篇文章。我认为真正的问题是,如何正确调用insert_batch并返回FALSE/Error ... – Berto

+0

哇,1.5个月后,这段代码仍然困扰着我。如果我只能从这个事情中得到一个错误......或者一个例外......或者只是一场死亡的崩溃。 – Berto

回答

1

我意识到这是一个老问题,但Codeigniter在第2节中仍然存在这个问题,所以希望这可以帮助某人。

无论出于何种原因,insert_batch()都不会以与标准查询相同的方式返回。一般情况下,你可能只是这样做:

if(!$this->CI->db->insert('product_images', $simpleArray) 
{ 
    error_log("\n\n".$this->CI->db->last_query()."\n"); 
} 

但正如原来的问题发现,insert_batch不会在失败的查询返回false。对于insert_batch(),这种解决方法已经很好地解决了我的应用程序的问题:

$this->CI->db->insert_batch('product_images', $master_insert_array); 

if($this->CI->db->_error_message() != '') 
{ 
    error_log("\n\n".$this->CI->db->last_query()."\n"); 
}