Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Alter table in custom query
  • I'm having an issue with database columns/fields. Whenever I insert a column using the DBUtil::add_fields() function, it always adds the field(s) at the end of the table no matter what. Within our system, we need to have these columns in a specific order within the table. Generally, I'd use an ALTER TABLE command in MySQL to do this, however I just can't figure out how to do this using FuelPHP's DB::query() function. It seems to only allow the use of INSERT, UPDATE, DELETE and DROP commands only. I should point out that I am using FuelPHP's migrations via OIL when I'm attempting this.

    If anyone knows of a simple way that I'm not seeing to do this, it would be greatly appreciated. Having new columns at the end of our tables is becoming quite annoying.
  • HarroHarro
    Accepted Answer
    An example:

    // add a database name column to support multiple clusters
    \DBUtil::add_fields('licensing_servers', array(
        'database_name' => array(
            'type' => 'varchar',
            'constraint' => '50',
            'default' => 'CoreApplications',
            'after' => 'database_type'
        ),
    ));

    likewise, you can also use 'before' instead of 'after'.
  • A million + 1 thank you's!! This worked perfectly. It wasn't in the documentation so I didn't even know if it was possible using Fuel or not.

    By chance, would you happen to know how to move a table column once it's already been created?
  • HarroHarro
    Accepted Answer
    I haven't tried it, but this should work:

    \DBUtil::modify_fields($table, array(
        'fieldname' => array('name' => 'fieldname', 'after' => 'otherfield'),
    ));

  • Thanks again for your help! I initially tried the "after" key in the modify_fields() function to no avail. But then I remembered that MySQL requires you to re-specify the column name and it's field type. Once I added that in, it worked like a charm.

    Thanks again for you help with this. It was driving me crazy. :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion