22

have one question, recently I was developing one module with a lot of tables in DB, and concept was changing often, so was need to change existing tables in DB, and I noticed difference in table creating script and table upgrading. Here you go. Look at creating table code below:

$table = $installer->getConnection()
    ->newTable($installer->getTable('module/table'))
    ->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, 9, array(
        'nullable' => false,
        'primary' => true,
        'identity' => true,
        'auto_increment' => true
    )
);

the newTable() function returns instance of Varien_Db_Ddl_Table And upgrading table script uses different way to add new column to existing table, take a look:

$installer->getConnection()
    ->addColumn($tableName, 'test', array(
        'nullable' => false,
        'length' => 9,
        'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
        'comment' => 'Test Field'
    )
)

these two addColumn functions are different and also they are methods of different classes, and they make me sad every time I need to change syntax.
So here is question, is there way to update existing table using instance of the Varien_Db_Ddl_Table class?

15

There does not appear to be a way to modify an existing table using the Varien_Db_Ddl_Table object. If you go into the code for that class you will see no area where it pulls in the existing schema for the table, or checks to even see if the table exists in any way. That would be required if you were to use it for modifying the table.

Additionally, in Varien_Db_Adapter_Interface there is no method along the lines of 'updateTable' that takes a Varien_Db_Ddl_Table object as a parameter.

This is definitely one of those 'code smells' in Magento, as you have two completely different blocks of code attempting to accomplish the same thing in different ways. Will only lead to bugs.

  • nice answer, I thought as much, thanks :) – Nick Jan 28 '13 at 15:59
  • 2
    And now make a Magento 2 pull request to fix it :-) – Alex Jan 28 '13 at 16:51
6

If it's within the scope of the project you might want to look into changing to an EAV model if the model is changing very frequently as you mentioned. This can save you the trouble of confusing data migrations back and forth. Here's an article that explains the basics of EAV in Magento so that you can evaluate it and decide if it's appropriate for your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.