2

Im using CodeIgniters DBforge to use my database.

Im trying to convert a plain PHP script to CodeIgniter and im having some trouble with changing the SQL to dbforge.

I have the following SQL code

CREATE TABLE `message2_recips` (
  `mid` int(10) unsigned NOT NULL,
  `seq` int(10) unsigned NOT NULL,
  `uid` int(10) unsigned NOT NULL,
  `status` char(1) NOT NULL default 'N',
  KEY `m2r1` USING BTREE (`mid`,`status`),
  KEY `m2r2` USING BTREE (`uid`,`status`)
) ENGINE=InnoDB;

Everything works fine except when i get to the 2 key values at the bottom.

Everything i have tried doesnt seem to work including,

$this->dbforge->add_key(array('mid', 'status'));
$this->dbforge->add_key(array('uid', 'status'));

Any help translating this would be greatly appreciated, i cant seem to find any way to input this using dbforge, which brings my project to a complete standstill

Thanks.

BigJobbies
  • 3,633
  • 11
  • 43
  • 66
  • Just out of curiosity, why not use the normal DB class and SQL to create the table? – Tim Withers Nov 09 '12 at 15:15
  • 1
    @TimWithers - For this project i thought i would try out migrations as its all source controlled and i wanted to deploy to various places without touching the DB ... I read that dbforge was the way to do it – BigJobbies Nov 09 '12 at 15:17
  • The CodeIgniter documentation says that: "...add_key() must be followed by a call to create_table()" - http://codeigniter.com/user_guide/database/forge.html Could that be causing the issue? Also, dbForge is really just a handy wrapper for SQL functionality - I've found that I've had to extend it a few times to build in the flexibility that I need. – adomnom Nov 13 '12 at 01:13
  • @adomnom Hey, when i run the add_key that i have in my post, it throws up an error saying that there are 2 status keys, except if you have a look at the raw SQL, they are named and i cant find this ... is there any way i can use raw SQL in the migration file to create the table instead of using codeigniters db wrapper? – BigJobbies Nov 13 '12 at 04:46

2 Answers2

1

Impossible to do (currently) via DB Forge - it doesn't support additional options for table keys. You can only achieve that by adding them afterwards with raw SQL queries.

Narf
  • 14,600
  • 3
  • 37
  • 66
1

There is a bug in DB_forge.php

You need to remove is_array condition to enable multiple keys (in mysql driver there is function to convert the array key to multiple key).

DBForge.php:101

Remove:

    if (is_array($key))
    {
        foreach ($key as $one)
        {
            $this->add_key($one, $primary);
        }

        return;
    }
Pion
  • 708
  • 9
  • 21