CodeIgniter Forums
Using dbforge? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Using dbforge? (/thread-34027.html)



Using dbforge? - El Forum - 09-16-2010

[eluser]chefnelone[/eluser]
Hello

I'm creating a new table using dbforge.

If I tried with this code, it works fine:
Code:
$fields = array(
                   'id' => array('type' => 'INT','auto_increment' => TRUE),
                   'date' => array('type' => 'TIMESTAMP')
        );
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table($tabla, TRUE);

But it FAILS if I tried to set the 'default' value to 'CURRENT_TIMESTAMP' for the field 'date'. Tried with:
Code:
...
           'date' => array('type' => 'TIMESTAMP', 'default' => 'CURRENT_TIMESTAMP' ") //FAILS.
...

How can I solve this?
thanks


Using dbforge? - El Forum - 09-16-2010

[eluser]n0xie[/eluser]
Did you try without the " ?
Code:
...
           'date' => array('type' => 'TIMESTAMP', 'default' => 'CURRENT_TIMESTAMP')
...



Using dbforge? - El Forum - 09-16-2010

[eluser]chefnelone[/eluser]
[quote author="n0xie" date="1284658880"]Did you try without the " ?
Code:
...
           'date' => array('type' => 'TIMESTAMP', 'default' => 'CURRENT_TIMESTAMP')
...
[/quote]

I tried, but.. no working.

Code:
...
           'date' => array('type' => 'TIMESTAMP', 'default' => CURRENT_TIMESTAMP)
...



Using dbforge? - El Forum - 09-16-2010

[eluser]WanWizard[/eluser]
It's not going to work, because the dbforge database driver assumes it's a literal (string, number), an places any default value in single quotes.

The work-around is:
Code:
'date' => array("date TIMESTAMP DEFAULT CURRENT_TIMESTAMP")

if you pass a string as array value instead of an creating an associated array, the string is used as-is. Note that it is likely that this breaks database portability.

This is explained in the user guide, database library, dbforge, add_field.


Using dbforge? - El Forum - 09-16-2010

[eluser]chefnelone[/eluser]
[quote author="WanWizard" date="1284669760"]It's not going to work, because the dbforge database driver assumes it's a literal (string, number), an places any default value in single quotes.

The work-around is:
Code:
'date' => array("date TIMESTAMP DEFAULT CURRENT_TIMESTAMP")

if you pass a string as array value instead of an creating an associated array, the string is used as-is. Note that it is likely that this breaks database portability.

This is explained in the user guide, database library, dbforge, add_field.[/quote]


hum.. something is wrong. I tried with: (both fail...)
Code:
'fecha' => array("fecha TIMESTAMP DEFAULT 'CURRENT_TIMESTAMP'")
Code:
'fecha' => array("fecha TIMESTAMP DEFAULT CURRENT_TIMESTAMP")



Using dbforge? - El Forum - 09-16-2010

[eluser]WanWizard[/eluser]
What's the definition of fail? Error messages? What's the output of $this->db->last_query()? What database engine are you using?


Using dbforge? - El Forum - 06-17-2011

[eluser]ipsod[/eluser]
This should work:
Code:
'date' => array('type=>"TIMESTAMP DEFAULT CURRENT_TIMESTAMP")

Note, though, MySQL table can only have a single field that has CURRENT_TIMESTAMP as default, according to errors.


Using dbforge? - El Forum - 08-02-2012

[eluser]Unknown[/eluser]
Not sure when this changed but...

to do this now (CI Version: 2.1.2), the code should be as follows:

Code:
$this->dbforge->add_field(array(
      'id' => array(
          'type' => 'INT',
          'constraint' => 11,
          'auto_increment' => TRUE
        ),
      'date TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
      'name' => array(
        'type' => 'VARCHAR',
        'constraint' => 255,
        'default' => 'John Smith'
      )
));

... with extra fields (id, name) to give context.

So you can pass the whole definition for a field as a string instead of passing an array.
Also - if you just pass the string 'id' it'll auto convert to :

Code:
'id' => array(
  'type' => 'INT',
  'constraint' => 9,
  'auto_increment' => TRUE
)