WordPress $ wpdb->更新set float为null?

时间:2012-12-12 02:35:47

标签: php wordpress

有没有办法使用NULL将列设置为$wpdb->update();

当我尝试时,WordPress会尝试将该列强制转换为浮点数,将NULL转换为0

我检查了核心代码和$wpdb->update()内部,$format参数只期望%s,%f和%d。我甚至将$wpdb->field_types['myCol']设置为'NULL',但两者都只用于打破$wpdb->update()的查询(有趣的是,它会在{{1}之后移动每列的值。 }})。

此处有一个相关问题,但该答案仅涉及NULL INSERT

从数据完整性的角度来看,UPDATE对于此列非常重要,因此我必须能够在必要时对其进行设置。

2 个答案:

答案 0 :(得分:1)

正如您自己所说,$format参数只期待%s%f%d。整个事情将通过prepare传递,它也不会采用null格式说明符,因为它基本上接受(v)(s)printf接受但没有参数交换的相同格式说明符。您无法通过NULL通过update。你有the post suggested as a possible duplicate中提到的相同选择,实质上它是。

  1. 只是不要使用update功能。编写自己的SQL并将其与$wpdb->query一起使用。如果您正在处理自己的表格,那应该没问题。
  2. 创建您自己的数据库类,执行您想要的操作。事实上,您可以用drop-in替换WordPress的$wpdb

答案 1 :(得分:0)

这是一个从最新版本的wordpress修改wpdb的解决方案,以便允许使用insert()和update()将空值插入和更新到SQL表中:

/*
 * Fix wpdb to allow inserting/updating of null values into tables
 */
class wpdbfixed extends wpdb
{
    function insert($table, $data, $format = null) {
        $type = 'INSERT';
        if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
            return false;
        $this->insert_id = 0;
        $formats = $format = (array) $format;
        $fields = array_keys( $data );
        $formatted_fields = array();
        foreach ( $fields as $field ) {
            if ( !empty( $format ) )
                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
            elseif ( isset( $this->field_types[$field] ) )
                $form = $this->field_types[$field];
            else
                $form = '%s';

            //***edit begin here***
            if ($data[$field]===null) {
                unset($data[$field]); //Remove this element from array, so we don't try to insert its value into the %s/%d/%f parts during prepare().  Without this, array would become shifted.
                $formatted_fields[] = 'NULL';
            } else {
                $formatted_fields[] = $form; //Original line of code
            }
            //***edit ends here***
        }
        $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
        return $this->query( $this->prepare( $sql, $data ) );
    }

    function update($table, $data, $where, $format = null, $where_format = null)
    {
        if ( ! is_array( $data ) || ! is_array( $where ) )
            return false;

        $formats = $format = (array) $format;
        $bits = $wheres = array();
        foreach ( (array) array_keys( $data ) as $field ) {
            if ( !empty( $format ) )
                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
            elseif ( isset($this->field_types[$field]) )
                $form = $this->field_types[$field];
            else
                $form = '%s';

            //***edit begin here***
            if ($data[$field]===null)
            {
                unset($data[$field]); //Remove this element from array, so we don't try to insert its value into the %s/%d/%f parts during prepare().  Without this, array would become shifted.
                $bits[] = "`$field` = NULL";
            } else {
                $bits[] = "`$field` = {$form}"; //Original line of code
            }
            //***edit ends here***
        }

        $where_formats = $where_format = (array) $where_format;
        foreach ( (array) array_keys( $where ) as $field ) {
            if ( !empty( $where_format ) )
                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
            elseif ( isset( $this->field_types[$field] ) )
                $form = $this->field_types[$field];
            else
                $form = '%s';
            $wheres[] = "`$field` = {$form}";
        }

        $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
        return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    }

}
global $wpdb_allow_null;
$wpdb_allow_null = new wpdbfixed(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

将此代码插入总是运行的地方,例如您的functions.php,然后正常使用新的全局$ wpdb_allowed_null-> insert()和 - > update()。

我更喜欢这样做而不是覆盖默认的$ wpdb,以保留其他Wordpress和其他插件所期望的数据库行为。