Tag Archives: addColumn

Magento: How to create a timestamp table column with CURRENT_TIMESTAMP as default value?

Well, the easiest answer would be to use raw queries to create such tables. But that is not the Magento style. We will use the Magento style to build our table.

So, I suppose that you already created your install / upgrade script in your module. Now you have something like:

$this->startSetup();

//...

$this->endSetup();

In many cases you will meet with $installer = $this; and  with $installer->startSetup(), both are the same thing (it was created like this a long time ago, when such a notation influenced the autocomplete of the IDEs). The create table statement will be between the startSetup and the endSetup like:

$table = $this->getConnection()->newTable("your_table_definition")
->addColumn("column_name", Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
"default" => "SOMETHING_DEFAULT"
), "some comment here")
->addColumn(...)
...
;
$this->getConnection()->createTable($table);

The question is what to put instead of SOMETHING_DEFAULT? Use Varien_Db_Ddl_Table::TIMESTAMP_INIT, so your addColumn definition will look like:

->addColumn("column_name", Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
"default" => Varien_Db_Ddl_Table::TIMESTAMP_INIT
), "some comment here")

That’s it.

Grid column types in Magento

I recently had to implement a new grid in Magento, and I wasn’t sure what should I write to the type parameter:

protected function _prepareColumns()
{
      $this->addColumn('some_column_id', array(
              'header' => Mage::helper('core')->__('Some column name'),
              'index' => 'some_column_index',
              'type' => '???',
      ));
...

So I made a little bit of research to find out all the types available. I don’t want to go in details, so here is the list:

  • action
  • checkbox
  • concat
  • country
  • currency
  • date
  • datetime
  • input
  • interface
  • ip
  • longtext
  • massaction
  • number
  • options
  • price
  • radio
  • select
  • store
  • text
  • theme
  • wrapline

You will find all these types in /app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer folder. You can also make your own grid types similarly.