0

I've created a module for ExpressionEngine and since it's initial installation I need to add a new column to the database. I've managed to create the update funtion in the upd.modulename.php file and it's working. The problem is the code contained to add the new column doesn't seem to be doing what I need it to.

public function update( $current = '' ) {
    ee()->load->dbforge();

    if ( $current == $this->version ) {
        return FALSE;
    }

    if ( $current < '1.1.0' && ! ee()->db->field_exists( 'new_field', 'module_table' ) ) {
        $updates = array(
            'new_field' => array(
                'type' => 'varchar',
                'constraint' => '250'
            )
        );
        ee()->dbforge->add_column( $updates );
    }

    return TRUE;
}

This conditional is working as I've managed to print out a message when running the module updates but it doesn't appear to be adding the new column to the table. I don't suppose anyone has any ideas?

2 Answers 2

2

add_column just defines the column specs. After which you need to run create_table

https://codeigniter.com/userguide2/database/forge.html#add_field

"After the fields have been defined, they can be added using $this->dbforge->add_field($fields); followed by a call to the create_table() function."

1
  • Unfortunately this didn't work for me, presumably it's because the table is already created. It did point me to the right direction though. I re-read the CodeIgniter instructions and realised I'd missed a vital part of the add_column function. Commented Dec 14, 2015 at 16:45
2

The add column function requires the database name, something that I had missed out in my function.

ee()->dbforge->add_column( 'module_table', $updates );

Full code:

public function update( $current = '' ) {
    ee()->load->dbforge();

    if ( $current == $this->version ) {
        return FALSE;
    }

    if ( $current < '1.1.0' && ! ee()->db->field_exists( 'new_field', 'module_table' ) ) {
        $updates = array(
            'new_field' => array(
                'type' => 'varchar',
                'constraint' => '250'
            )
        );
        ee()->dbforge->add_column( 'module_table', $updates ); //Added table name here
    }

    return TRUE;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.