Fuelphp: convert standard ORM model to model_temporal

118 Views Asked by At

Using fuelphp, is there a way to modify an existing \ORM\Model to convert it to \ORM\Model_Temporal? I started looking into creating a migration, which needs to 1) add the necessary columns to my table (not difficult); 2) update the primary key to include the new columns as part of a compound primary key (difficult). The problem I am having is that I don't see a method in the DBUtil that performs this operation.

What is the right way to do this?

2

There are 2 best solutions below

0
Emlyn On BEST ANSWER

Currently DBUtil does not include a way to alter primary keys because the method of doing so differs between DB systems and DBUtil is not actually that aware what DBMS you are using.

To do this you will have to construct the query manually in your migration and use DB::query() to execute it.

0
tmpearce On

In case anyone else is faced with the same task, here is what I ended up doing. My database is MySQL, the syntax may have to be changed if you're using a different DBMS. My original primary key was an auto-incrementing column named id.

class Modify_TableName_To_Temporal
{
    public function up()
    {
        \DBUtil::add_fields('tablename',array(
            'temporal_start'=>array('constraint'=>11, 'type'=>'int','default'=>0),
            'temporal_end'=>array('constraint'=>11, 'type'=>'int','default'=>2147483647),
        ));
        \DB::query('ALTER TABLE tablename MODIFY id int, DROP PRIMARY KEY')->execute();
        \DB::query('ALTER TABLE tablename ADD PRIMARY KEY (id, temporal_start, temporal_end), MODIFY id int auto_increment')->execute();
    }

    public function down()
    {
        \DB::query('ALTER TABLE tablename MODIFY id int, DROP CONSTRAINT primary')->execute();
        \DB::query('ALTER TABLE tablename ADD PRIMARY KEY (id), MODIFY id int auto_increment')->execute();
        \DBUtil::drop_fields('tablename',array('temporal_start','temporal_end'));
    }
}