Question

Trying to create a custom module with an installer script, I followed this tutorial (with a few edits): http://inchoo.net/magento/magento-install-install-upgrade-data-and-data-upgrade-scripts/

I've tried running the code and it doesn't work :(

here's my config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.3.0</version>
        </Namespace_Module>
    </modules>

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <namespace_module before="Mage_Adminhtml">Namespace_Module_Adminhtml</namespace_module>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

    <adminhtml>
        <layout>
            <updates>
                <namespace_module>
                    <file>videocommerce.xml</file>
                </namespace_module>
            </updates>
        </layout>

        <events>
            <catalog_product_save_after>
                <observers>
                    <namepsace_module_save_after>
                        <type>singleton</type>
                        <class>namepsace_module/observer</class>
                        <method>saveVideoTabData</method>
                    </namepsace_module_save_after>
                </observers>
            </catalog_product_save_after>
        </events>
    </adminhtml>

    <global>
        <helpers>
            <namepsace_module>
                <class>Namepsace_Module_Helper</class>
            </namepsace_module>
        </helpers>

        <blocks>
            <namepsace_module>
                <class>Namepsace_Module_Block</class>
            </namepsace_module>
        </blocks>

        <models>
            <namepsace_module>
                <class>Namepsace_Module_Model</class>
                <resourceModel>namespace_module_resource</resourceModel>
            </namepsace_module>

            <namepsace_module_resource>
                <class>Namepsace_Module_Model_Resource</class>
                <entities>
                    <video_category>
                        <table>namepsace_module_video_categories</table>
                    </video_category>

                    <product_videos>
                        <table>namepsace_module_product_videos</table>
                    </product_videos>
                </entities>
            </namepsace_module_resource>
        </models>

        <models>
            <namepsace_module>
                <class>Namepsace_Module_Model</class>
                <resourceModel>namepsace_module_resource</resourceModel>
            </namepsace_module>

            <namepsace_module_resource>
                <class>Namepsace_Module_Model_Resource</class>
                <entities>
                    <video_category>
                        <table>namepsace_module_video_categories</table>
                    </video_category>

                    <product_videos>
                        <table>namepsace_module_product_videos</table>
                    </product_videos>
                </entities>
            </namepsace_module_resource>
        </models>

        <resources>
            <namepsace_module_setup>
                <setup>
                    <module>Namepsace_Module</module>
                </setup>
            </namepsace_module_setup>

            <namepsace_module_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </namepsace_module_write>

            <namepsace_module_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </namepsace_module_read>
        </resources>
    </global>
</config>

my data-install-0.3.0.php script:

<?php
    $defaultCategories = array(
        array(
            'category_name' => 'Category 1',
            'enabled' => true
        ),
        array(
            'category_name' => 'Category 2',
            'enabled' => true
        ),
        array(
            'category_name' => 'Category 3',
            'enabled' => true
        )
    );

    foreach ($defaultCategories as $category) {
        Mage::getModel('namespace_module/videocategory')
            ->setData($category)
            ->save();
    }

and my mysql4-install-0.3.0.php:

<?php
    $installer = $this;
    $installer->startSetup();

    $table = $installer->getConnection()
        ->newTable($installer->getTable('namespace_module/video_categories'))
        ->addColumn('category_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'identity'  => true,
            'unsigned'  => true,
            'nullable'  => false,
            'primary'   => true,
        ), 'Category Id')
        ->addColumn('category_name', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
            'nullable'  => false,
        ), 'Category Name')
        ->addColumn('enabled', Varien_Db_Ddl_Table::TYPE_BOOLEAN, null, array(
            'nullable'  => false,
        ), 'Enabled');

    $installer->getConnection()->createTable($table);

    $table = $installer->getConnection()
        ->newTable($installer->getTable('namespace_module/product_videos'))
        ->addColumn('video_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'identity'  => true,
            'unsigned'  => true,
            'nullable'  => false,
            'primary'   => true
        ), 'Video Id')
        ->addColumn('cat_id_fk', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'identity'  => true,
            'unsigned'  => true,
            'nullable'  => false,
        ), 'Category Id')
        ->addColumn('video_iframe', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
            'nullable' => true
        ), 'Video Iframe')
        ->addForeignKey(
            $installer->getFkName('product_videos', 'category_id', 'video_categories', 'category_id'),
            'category_id',
            $installer->getTable('video_categories'),
            'category_id',
            Varien_Db_Ddl_Table::ACTION_CASCADE,
            Varien_Db_Ddl_Table::ACTION_CASCADE
        );

    $installer->getConnection()->createTable($table);
    $installer->endSetup();

And File Tree:

| - NAMESPACE
    | - Module
        | - Block
            | - Adminhtml
                | - Catalog
                    | - Product
                        | - Tab.php
        | - controllers
            | - Adminhtml
                | - IndexController.php
        | - etc
            | - adminhtml.xml
            | - config.xml
        | - Helper
            | - Data.php
        | - Model
            | - Resource
                | - ProductVideos
                    | - Collection.php
                | - VideoCategories
                    | - Collection.php
                | - ProductVideos.php
                | - VideoCategories.php
            | - ProductVideos.php
            | - VideoCategories.php
        | - sql
            | - data-install-0.3.0.php
            | - mysql4-install-0.3.0.php
Was it helpful?

Solution

I think you missed a folder inside sql.

You have to create a folder named namepsace_module_setup and then place all your script inside this folder.

So your sql folder structure will be:

   sql
     |_ namepsace_module_setup
            |_ your script files

Also, if you find namespace_module_setup in your core_resource table then you will have to remove this, so that your code runs again.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top