Welcome Guest, Not a member yet? Register   Sign In
Forge Class bug in numeric field name
#1

If you set a field name in table that is a numeric value, because array_merge in DB_forge.php function add_field, this numeric names are transformed into array index, making wrong result.

Code:
$fields = array(
                '0000' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0100' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0200' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0300' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0400' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0500' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0600' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0700' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0800' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '0900' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1000' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1100' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1200' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1300' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1400' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1500' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1600' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1700' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1800' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '1900' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '2000' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '2100' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '2200' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
                '2300' => array(
                        'type' => 'BIT',
                        'constraint' => '1',
                        'null' => FALSE,
                        'default' => 0,
                ),
        );

        f( !$this->dbforge->add_field( $fields ) )
            return $this->db->error();


Return:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0000 BIT(1) NOT NULL DEFAULT 0,   0100 BIT(1) NOT NULL DEFAULT 0, 0200 BIT(1) NO'


Dump of SQL:

Code:
CREATE TABLE IF NOT EXISTS `schedule_time` (
0000 BIT(1) NOT NULL DEFAULT 0,
0100 BIT(1) NOT NULL DEFAULT 0,
0200 BIT(1) NOT NULL DEFAULT 0,
0300 BIT(1) NOT NULL DEFAULT 0,
0400 BIT(1) NOT NULL DEFAULT 0,
0500 BIT(1) NOT NULL DEFAULT 0,
0600 BIT(1) NOT NULL DEFAULT 0,
0700 BIT(1) NOT NULL DEFAULT 0,
0800 BIT(1) NOT NULL DEFAULT 0,
0900 BIT(1) NOT NULL DEFAULT 0,
0 BIT(1) NOT NULL DEFAULT 0,
`1` BIT(1) NOT NULL DEFAULT 0,
`2` BIT(1) NOT NULL DEFAULT 0,
`3` BIT(1) NOT NULL DEFAULT 0,
`4` BIT(1) NOT NULL DEFAULT 0,
`5` BIT(1) NOT NULL DEFAULT 0,
`6` BIT(1) NOT NULL DEFAULT 0,
`7` BIT(1) NOT NULL DEFAULT 0,
`8` BIT(1) NOT NULL DEFAULT 0,
`9` BIT(1) NOT NULL DEFAULT 0,
`10` BIT(1) NOT NULL DEFAULT 0,
`11` BIT(1) NOT NULL DEFAULT 0,
`12` BIT(1) NOT NULL DEFAULT 0,
`13` BIT(1) NOT NULL DEFAULT 0
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci 


Expected SQL:

Code:
CREATE TABLE `schedule_time` (
    `0000` BIT NOT NULL DEFAULT 0,
    `0100` BIT NOT NULL DEFAULT 0,
    `0200` BIT NOT NULL DEFAULT 0,
    `0300` BIT NOT NULL DEFAULT 0,
    `0400` BIT NOT NULL DEFAULT 0,
    `0500` BIT NOT NULL DEFAULT 0,
    `0600` BIT NOT NULL DEFAULT 0,
    `0700` BIT NOT NULL DEFAULT 0,
    `0800` BIT NOT NULL DEFAULT 0,
    `0900` BIT NOT NULL DEFAULT 0,
    `1000` BIT NOT NULL DEFAULT 0,
    `1100` BIT NOT NULL DEFAULT 0,
    `1200` BIT NOT NULL DEFAULT 0,
    `1300` BIT NOT NULL DEFAULT 0,
    `1400` BIT NOT NULL DEFAULT 0,
    `1500` BIT NOT NULL DEFAULT 0,
    `1600` BIT NOT NULL DEFAULT 0,
    `1700` BIT NOT NULL DEFAULT 0,
    `1800` BIT NOT NULL DEFAULT 0,
    `1900` BIT NOT NULL DEFAULT 0,
    `2000` BIT NOT NULL DEFAULT 0,
    `2100` BIT NOT NULL DEFAULT 0,
    `2200` BIT NOT NULL DEFAULT 0,
    `2300` BIT NOT NULL DEFAULT 0,
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci
Reply
#2

I don't know if this is an issue but your missing a "i" in your if statement. Is that a mistake?
Reply
#3

(This post was last modified: 01-19-2017, 04:14 AM by Rodrigo Valentim.)

(01-18-2017, 12:37 PM)php_rocs Wrote: I don't know if this is an issue but your missing a "i" in your if statement.  Is that a mistake?

Was a typo in cut and past.

I fixed the add_field in DB_forge.php for my use, and now is fine, but may have a problem in some situations...
Code:
        if (is_array($field))
        {
            //$this->fields = array_merge($this->fields, $field);
            foreach ($field as $k => $v) {
                $this->fields[$k] = $v;
            }
        }



And after that I need to fix the _process_fields function too, because of the same problem, this I think could be a "ok" sollution:
Code:
$field = array(
                //'name'            => $key,
                'name'            => ( is_numeric($key) ? '`' . $key . '`' : $key ),
                'new_name'        => isset($attributes['NAME']) ? $attributes['NAME'] : NULL,
                'type'            => isset($attributes['TYPE']) ? $attributes['TYPE'] : NULL,
                'length'        => '',
                'unsigned'        => '',
                'null'            => '',
                'unique'        => '',
                'default'        => '',
                'auto_increment'    => '',
                '_literal'        => FALSE
            );
Reply




Theme © iAndrew 2016 - Forum software by © MyBB