Magento 1.9 – Update Table in MySQL Setup Script

databasemagento-1.9scriptupgrade

Since couple of weeks I'm trying to understand Magento, it's structure, mechanisms and I'm stuck on scripts creating for example additional columns in tables.
In my config.xml script I have:

<?xml version="1.0"?>

<config >
< modules >
    < Inchoo_Orders >
        <version>0.0.0.3< /version>
    < /Inchoo_Orders>
< /modules>
<global>
    <models>
        <inchoo_orders>
            <class>Inchoo_Orders_Model</class>
enter preformatted text here          
  <resourceModel>inchoo_orders_resource</resourceModel>
        </inchoo_orders>
    </models>
    <resources>
        <inchoo_orders_setup>
            <setup>
                <module>Inchoo_Orders</module>
                <class>Inchoo_Orders_Model_Mysql4_Setup</class>
            </setup>
        </inchoo_orders_setup>
    </resources>
   ......

My Sql script name is: mysql4-upgrade-0.0.0.2-0.0.0.3.php

But it won't update table. What I'm doing wrong ?
Getting error:

Fatal error: Class 'Inchoo_Orders_Model_Mysql4_Setup' not found in
/home/…/app/code/core/Mage/Core/Model/Resource/Setup.php
on line 234

If I change class to Mage_Core_Model_Resource_Setup not giving me any errors but also I can't see it work.

Best Answer

Mysql4 is actually deprecated. However, if your Mysql4_Setup file lives within the resource directory, you will need to change the class name to Inchoo_Orders_Model_Resource_Mysql4_Setup.

If you are just trying to update a table and not much else, you should be able to just change

<class>Inchoo_Orders_Model_Mysql4_Setup</class>

to

<class>Mage_Core_Model_Resource_Setup</class>

If you need some sort of custom functionality that will live in your own Setup class, create this file.

<?php

class Inchoo_Orders_Model_Resource_Setup extends Mage_Core_Model_Resource_Setup
{
   //whatever needs to go in here
} 

And remember to add this to your config.xml file:

<class>Inchoo_Orders_Model_Resource_Setup</class>

If you need instruction on how to actually update the table itself, let me know.


Based on our continued discussion in the comments, it has been determined that all you really want to do here is add an attribute programatically.

First things first, you do not need your install script should be an instance of Mage_Catalog_Model_Resource_Setup

<?php

/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();

$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'offer_status', array(
    'type'         => 'int',
    'label'        => 'Offer Status',
    'input'        => 'select',
    'global'       => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'source'       => 'inchoo_orders/catalog_product_attribute_source_offer_status',
    'backend'      => '',
    'visible'      => true,
    'required'     => false,
    'user_defined' => false,
    'default'      => '',
    'group'        => "General Information"
));

Then you need to create a source model for your attribute.

<?php

class Inchoo_Orders_Model_Catalog_Product_Attribute_Source_Offer_Status
{
    /**
     * Options getter
     *
     * @return array
     */
    public function toOptionArray()
    {
        return array(
            array('value' => 0, 'label' => Mage::helper('core')->__('Auto')),
            array('value' => 1, 'label' => Mage::helper('core')->__('Enabled')),
            array('value' => 2, 'label' => Mage::helper('core')->__('Disabled')),
        );
    }

    /**
     * Get options in "key-value" format
     *
     * @return array
     */
    public function toArray()
    {
        return array(
            0 => Mage::helper('core')->__('Auto'),
            1 => Mage::helper('core')->__('Enabled'),
            2 => Mage::helper('core')->__('Disabled'),
        );
    }
}
Related Topic