ALTER TABLE in Magento setup script without using SQL

前端 未结 3 1388
逝去的感伤
逝去的感伤 2020-11-28 01:20

Jonathon Day says

\"updates SHOULD NOT be in the form of SQL commands\". I haven\'t come across any DDL or DML statments that cannot be executed

3条回答
  •  春和景丽
    2020-11-28 01:45

    To alter table and add column with a foreign key, I have used this successfully using Magento CE v1.6.1.0 :

    // Alter table to add column
    $installer->getConnection()
    
            ->addColumn(
                $installer->getTable('modulekey/model'), 
                'column_name',  
                array(
                    'type'      => Varien_Db_Ddl_Table::TYPE_INTEGER,
                    'length'    => null,
                    'unsigned'  => true,
                    'nullable'  => true,
                    'comment'   => 'Foreign key'
                )
            );
    
    // Add foreign key constraint
    $installer->getConnection()
    
            ->addForeignKey(
                $installer->getFkName( 
                    'modulekey/model',  'column_name',
                    'modulekey/foreign_model',  'foreign_column_name'
                ),
                $installer->getTable('modulekey/model'), 
                'column_name',
                $installer->getTable('modulekey/foreign_model'),
                'foreign_column_name',
                Varien_Db_Ddl_Table::ACTION_SET_NULL, 
                Varien_Db_Ddl_Table::ACTION_SET_NULL
            );
    

    Those are methods from Varien_Db_Adapter_Pdo_Mysql.

提交回复
热议问题