0

我正在编写一个插件并尝试在 foreach 循环内的 wp_term_relationships 表中插入一个新行。由于 var_dump,我知道变量具有值,但由于某种原因,我一直收到错误。这在 show_errors() 函数上显示了大约 600 次:

WordPress 数据库错误:[键 1 的重复条目 '0-0'] INSERT INTO wp_term_relationships ( object_id, term_taxonomy_id, term_order) VALUES ('','','')

我的代码:

foreach ($cb_t2c_cat_check as $values) {
        global $wpdb;
        $prefix = $wpdb->prefix;

        $table = $prefix . 'term_relationships';
        $object_id = $values->object_id;
        $taxo_id = $values->term_taxonomy_id;
        $num_object_id = (int)$object_id;
        $num_taxo_id = (int)$taxo_id;

        //var_dump($num_object_id); //This produces values, so why are they not getting inserted into the table?
        //var_dump($num_taxo_id); //This produces values, so why are they not getting inserted into the table?

        $wpdb->insert( 
            $table, 
            array( 
                'object_id' => $num_object_id, 
                'term_taxonomy_id' => $num_taxo_id,
                'term_order' => 0
                ), '' 
            ); 

        //$wpdb->show_errors();
        //$wpdb->print_error();
        }
4

2 回答 2

1

至于为什么不起作用:不要将第三个参数设置$wpdb->insert为空字符串。它相应地格式化每个字段..

它现在所做的相当于:

$wpdb->insert($table, array(
            'object_id' => sprintf('', $num_object_id), 
            'term_taxonomy_id' => sprintf('', $num_taxo_id),
            'term_order' => sprintf('', 0)
));

如果你真的想设置第三个参数,你应该这样做:

$wpdb->insert($table, array(
            'object_id' => $num_object_id, 
            'term_taxonomy_id' => $num_taxo_id,
            'term_order' => 0
), array('%d', '%d', '%d'));

至于错误:wp_term_relationships 表在(object_id,term_taxonomy_id)上有一个唯一的主键。这意味着您不能在该表中有两行同时具有相同的 object_id 和 term_taxonomy_id。

尽管发生这种情况是因为通过将 insert 的第三个参数设置为空字符串,您试图一遍又一遍地插入 object_id=0 和 term_taxonomy_id=0 的行。

于 2012-02-24T11:39:21.810 回答
0

上面的答案是正确的,因为数据库需要有唯一的键,并且不能插入已经存在键值对的行,并且需要设置每个新值​​的格式。此外,特定于 Wordpress,还有一个我没有解决的问题,特别是处理 term_taxonomy 表和更新计数。

首先,重要的是要注意该插件旨在更新 term_relationships 表中帖子的某些类别。这实际上是使用 $wpdb-> insert 方法完成的。但是,我确定插件是否真的在 term_relationships 表中插入新行的测试不是直接看表,而是去 Wordpress 仪表板,选择类别,看看该类别的帖子数量是否超过前。这不起作用,因为插件从未更新 term_taxonomy 表中的计数。我只是通过单击 Wordpress 仪表板中某个类别旁边的“查看”并看到该类别的多个帖子才发现这一点,尽管官方的 Wordpress“计数”说没有。

我确认 term_taxonomy 表,即“count”列,也需要通过直接进入数据库并将 WHERE = 'term_taxonomy_id' 放入语句中进行更新。果然,有超过 1700 个结果,尽管 Wordpress 认为没有。

课程:使用 PHPMyAdmin 确认 $wpdb->insert 方法是否正常工作,而不必依赖 Wordpress 仪表板。

经过一些修改,代码现在运行良好。这是一个例子:

foreach ($cb_t2c_objects as $values) {
        global $wpdb;
        $prefix = $wpdb->prefix;

        $table = $prefix . 'term_relationships';
        $object_id = $values->object_id;
        $taxo_id = $values->cat_taxo;
        $num_object_id = (int)$object_id;
        $num_taxo_id = (int)$taxo_id;

        //Need to check to see if row exists for each, if not, then insert.
        $cb_t2c_get_row = $wpdb->get_row("
            SELECT * 
            FROM ".$prefix."term_relationships
            WHERE object_id = ".$num_object_id." AND term_taxonomy_id = ".$num_taxo_id."
            GROUP BY object_id, term_taxonomy_id
        ", OBJECT);

        //var_dump($cb_t2c_get_row);

        if ( is_null($cb_t2c_get_row) ) {
            //Insert the new values.
            $wpdb->insert( 
            $table, 
            array( 
                'object_id' => $num_object_id, 
                'term_taxonomy_id' => $num_taxo_id,
                'term_order' => 0
                ), 
            array(
                '%d', 
                '%d', 
                '%d'
                ) 
            );
        }

        //Set the variables for the count update.
        $cb_t2c_term_taxonomy_table = $prefix . 'term_taxonomy';
        $cb_t2c_update_data = $wpdb->get_var("
            SELECT count(term_taxonomy_id) as 'new_count'
            FROM ".$prefix."term_relationships
            WHERE term_taxonomy_id = ".$num_taxo_id."
        ",0,0); //returning NULL

        //var_dump($cb_t2c_update_data);

        //Update the count in the term_taxonomy table.
        $wpdb->query("
            UPDATE ".$prefix."term_taxonomy
            SET count = ".$cb_t2c_update_data."
            WHERE term_taxonomy_id = ".$num_taxo_id."
        ");
于 2012-02-27T00:37:24.367 回答