Php CodeIgniter:从自定义库插入批处理-失败,没有返回或错误-如何调试?

Php CodeIgniter:从自定义库插入批处理-失败,没有返回或错误-如何调试?,php,codeigniter,activerecord,batch-insert,Php,Codeigniter,Activerecord,Batch Insert,我有一个定制的CodeIgniter库,在这里我需要运行一些数据库操作 为了访问数据库,我执行以下命令,这是我在StackOverflow上学到的: $this->CI =& get_instance(); 我知道它在工作,因为这个命令有效: $drop_query = "DELETE FROM product_images WHERE product_id = " . $this->CI->db->escape($product_id); $this->

我有一个定制的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.
但是,插入批处理根本不起作用。没有返回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\u id未通过外键约束,导致批插入失败*

第二个错误日志在失败后永远不会运行。剧本刚刚结束

    $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");
那么,如何让insert_批处理正确地返回FALSE、错误或除完全失败以外的其他内容呢

*更新:我也尝试过将其放入try/catch块,但没有成功。如果它未能通过外键约束,它将中止我的整个脚本。我目前的观点是,insert_batch是一个写得很差的函数*


谢谢

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

无论出于何种原因,
insert\u batch()
的返回方式与标准查询不同。通常,您可以这样做:

if(!$this->CI->db->insert('product_images', $simpleArray)
{
    error_log("\n\n".$this->CI->db->last_query()."\n");
}
但正如在原始问题中发现的,insert_批处理不会在失败的查询中返回false。对于
insert\u 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");
}

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

无论出于何种原因,
insert\u batch()
的返回方式与标准查询不同。通常,您可以这样做:

if(!$this->CI->db->insert('product_images', $simpleArray)
{
    error_log("\n\n".$this->CI->db->last_query()."\n");
}
但正如在原始问题中发现的,insert_批处理不会在失败的查询中返回false。对于
insert\u 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");
}

在最后一个
错误日志之前使用
echo$this->db->last\u query()
查看输出。这不起作用,但结果是,我的$product\u id未通过外键约束。所以这是一个愚蠢的问题。很抱歉。马上就要更新这篇文章了。我认为真正的问题是,如何正确调用insert_batch并返回一个FALSE/错误…哇,1.5个月后,这段代码仍然困扰着我。要是我能从这件事上得到一个错误就好了。。。或者是一个例外。。。或者除了死机以外的任何东西。在最后一个
错误日志之前使用
echo$this->db->last\u query()
并查看输出。这不起作用,但事实证明,我的$product\u id未通过外键约束。所以这是一个愚蠢的问题。很抱歉。马上就要更新这篇文章了。我认为真正的问题是,如何正确调用insert_batch并返回一个FALSE/错误…哇,1.5个月后,这段代码仍然困扰着我。要是我能从这件事上得到一个错误就好了。。。或者是一个例外。。。或者除了一次致命的撞击。