代码点火器:从自定义库中insert_batch - 失败,没有返回或错误 - 如何调试


CodeIgniter: insert_batch from Custom Library - Fails with no Return or Error - How to Debug?

我有一个自定义的CodeIgniter库,我需要在其中运行一些数据库操作。

为了访问数据库,我执行了这个命令,这是我在StackOverflow上学到的:

$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根本不起作用。 没有返回 FALSE,没有错误...不。 它只是死了。

    $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正确地向我返回 FALSE、错误或其他内容,而不是完全失败?

*更新:我也尝试将其放入尝试/捕获块中,但没有成功。 如果它失败外键约束,它将中止我的整个脚本。 我目前认为insert_batch是一个写得不好的函数*

谢谢!

我意识到这是一个老问题,但 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");
}