23

How can I add a new column to existing Magento core table via install script? (without using pure SQL)

I want to use the Magento way which is using alias methods to create install script.

So far I followed few tutorials. But seems to be not working properly. This StackOverflow ALTER TABLE in Magento setup script without using SQL answer was somewhat similar to my question. But what is the content should be put into module's confg.xml file? Do I need to just define resource model, model and setup data would be enough?

The relevant portion of the config.xml (of my module) is as follow.

<config>
 . . . 
<global>
        <models>
            <mymodule>
                <class>Mynamespace_Mymodule_Model</class>
                <resourceModel>mymodule_resource</resourceModel>
            </mymodule>
            <mymodule_resource>
                <class>Mynamespace_Mymodule_Model_Resource</class>
            </mymodule_resource>
        </models>
        <resources>
            <mymodule_setup>
                <setup>
                    <module>Mynamespace_Mymodule</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </mymodule_setup>
            <mymodule_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </mymodule_read>
            <mymodule_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </mymodule_write>
        </resources>
. . . .
</config>

And my install script is as follows.

$installer = $this;
$installer->startSetup();

$installer->getConnection()
        ->addColumn($installer->getTable('sales_flat_order'),'custom_value', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
        'nullable'  => false,
), 'Title');
$installer->endSetup();

But I'm getting the following error.

SQLSTATE[42S02]: Base table or view not found: 1146 Table '255.sales_flat_order' doesn't exist

Any suggestion to fix this would be appreciated.

  • Is the databasename 255? – Fabian Blechschmidt May 3 '15 at 10:56
  • no. database name is something else. – Sukeshini May 3 '15 at 15:01
45

sales_flat_order is the full name of the table and so you have to use alias in $installer->getTable()

In $installer->getTable() parameter like module_alias/table_alias.

In that case try with

$installer->getTable('sales/order')

When you write this it will return table name sales_flat_order

because

module_alias = sales

table_alias = order

EDIT

You can use below script to add new column. It works fine in my system

$installer = $this;
$installer->startSetup();

$installer->getConnection()
->addColumn($installer->getTable('sales/order'),'custom_value', array(
    'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
    'nullable'  => false,
    'length'    => 255,
    'after'     => null, // column name to insert new column after
    'comment'   => 'Title'
    ));   
$installer->endSetup();

I am using Varien_Db_Ddl_Table::TYPE_TEXT insted of Varien_Db_Ddl_Table::TYPE_VARCHAR because TYPE_VARCHAR is deprecated

You can check @ Varien_Db_Adapter_Pdo_Mysql::$_ddlColumnTypes

And If you specify type TYPE_TEXT but set length to lets say 255 Magento will create a MySQL column of VARCHAR type.

  • tried this, Even though I'm getting the same response – Sukeshini May 3 '15 at 19:25
  • @Sukeshini check my edit.... – Keyur Shah May 4 '15 at 5:54
  • Thank you very much. It worked perfectly and +1 for the effort you made to solve my issue. – Sukeshini May 4 '15 at 16:40
  • glad to hear that it works for you @Sukeshini – Keyur Shah May 4 '15 at 17:09
  • @KeyurShah how to add a new field into custom table ? – Butterfly Jan 6 '19 at 13:50
5

You are misusing the method addColumn:

public function addColumn($tableName, $columnName, $definition, $schemaName = null)

the fourth parameter is the schemaName, in your call, the fourth parameter is 255.

->addColumn($installer->getTable('sales_flat_order'),'custom_value', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255, array(
    'nullable'  => false,
), 'Title')

If you use the right parameters, it should work.

  • 1
    TYPE_VARCHAR is deprecated so we have to use TYPE_TEXT as per Varien_Db_Adapter_Pdo_Mysql::$_ddlColumnTypes .,,correct me if i am wrong. @Fabian – Keyur Shah May 4 '15 at 5:56
  • You are right. TYPE_TEXT with a length < 255 maxes automatically a varchar. – Fabian Blechschmidt May 4 '15 at 6:15
  • @Fabian Blechschmidt: Thanks for pointing out the missed places. and +1 – Sukeshini May 4 '15 at 16:39
4

I realize this is a relatively 'old' question, but as it's still pretty find-able by Google, I decided to add this bit of information.

Regarding your question, if you want to change the sales/order table, this should not be done by the traditional install scripts/setup. The Mage_Catalog module uses a different Resource_Setup class, namely Mage_Sales_Model_Resource_Setup.

If you want to add attributes to the sales/order model, in order to ensure everything is added and processed correctly, add your attribute like this:

<?php

// Start setup
/** @var Mage_Sales_Model_Resource_Setup $installer */
$installer = new Mage_Sales_Model_Resource_Setup('core_setup');
$installer->startSetup();

// Gather info
$entityType = 'order'; // Adding attribute to this entity type (must be written out in text, not the entity type ID!! That'll not work.
$attributeName = 'my_attribute_code'; // Your attribute code/name

// Add attribute, very few parameters are accepted.
$installer->addAttribute($entityType, $attributeName, array(
    'type'              => 'varchar'
));

// End setup
$installer->endSetup();

Should you wonder why, then the answer is within the addAttribute() function of the Mage_Sales_Model_Resource_Setup class:

/**
 * Add entity attribute. Overwrited for flat entities support
 *
 * @param int|string $entityTypeId
 * @param string $code
 * @param array $attr
 * @return Mage_Sales_Model_Resource_Setup
 */
public function addAttribute($entityTypeId, $code, array $attr)
{
    if (isset($this->_flatEntityTables[$entityTypeId]) &&
        $this->_flatTableExist($this->_flatEntityTables[$entityTypeId]))
    {
        $this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
        $this->_addGridAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr, $entityTypeId);
    } else {
        parent::addAttribute($entityTypeId, $code, $attr);
    }
    return $this;
}
  • Good Catch.. Never know we can use addAttribute for Sales_Order module – Rajeev K Tomy Nov 2 '16 at 6:06
  • This is actually the most correct answer. – fantasticrice Mar 20 '19 at 4:00
  • I was using $this installer, your answer pointed me to the right direction. Thank you so much! – Dr. Gianluigi Zane Zanettini Nov 20 '19 at 10:12
0

simply you can create a script and in you dbscripts folder and run this file from terminal or web browser.

e.g save file in /dbscripts/filename.php paste this code and change according to your requirement

 <?php

require_once '../app/Mage.php';
//ini_set('display_errors', 1);
//umask(0);

Mage::app();

/** @var Mage_Core_Model_Resource_Setup $installer */

/*mysql4-install-0.1.0.php*/
$installer = new Mage_Core_Model_Resource_Setup('core_setup');

$installer->startSetup();
//echo $installer->getTable('maxoptra/maxoptratable');
//die("here");
$installer->run("
    ALTER TABLE `Table_name` ADD `New_Column_name` TEXT NOT NULL AFTER `other_exiting_column_name`");

$installer->endSetup();

echo"Script Run Successfully";

run this file from browser like

domain.name/dbscripts/filename.php

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.