3

I need to "debug"(basically see what's happening) inside the mysql4-install.php, because I have some problems to retrieve/insert new values in the just created tables.

<?php
//Version 0.1.0
    Mage::log('start', null, 'mysqlinstaller.log');
    $installer=$this;
    $installer->startSetup();

    $installer->run("
            DROP TABLE IF EXISTS `{$installer->getTable('country/product')}`;
            CREATE TABLE `{$installer->getTable('country/product')}` (
              `product_id` INT(11) unsigned NOT NULL,
              `active` ENUM('0','1') NOT NULL DEFAULT '0',
              `allowed` ENUM('0','1') NOT NULL  DEFAULT '0',
              `country` VARCHAR(255),
              PRIMARY KEY (`product_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        ");

    $installer->run("
            DROP TABLE IF EXISTS `{$installer->getTable('country/category')}`;
            CREATE TABLE `{$installer->getTable('country/category')}` (
              `category_id` INT(11) unsigned NOT NULL,
              `active` ENUM('0','1') NOT NULL DEFAULT '0',
              `allowed` ENUM('0','1') NOT NULL DEFAULT '0',
              `country` VARCHAR(255),
              PRIMARY KEY (`category_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        ");

    $setup = new Mage_Core_Model_Config();
    $setup->saveConfig('razorphyn/country/buttons', json_encode(array()), 'default', 0); 


    //NEED TO KNOW WHAT IS GOING FROM HERE


    //Populate table country/product
    $collection = Mage::getModel('catalog/product')->getCollection()->getAllIds();

    foreach($collection as $res){
        $data = array('product_id'=>$res,'active'=>0,'allowed'=>0,'country'=>'');
        Mage::getModel('country/product')->setData($data)->save();
    }

    //Populate table country/catalog
    $collection = Mage::getModel('catalog/category');
    $catTree = $collection->getTreeModel()->load();
    $catIds = $catTree->getCollection()->getAllIds();

    foreach($catIds as $id){
        $data = array('category_id'=>$id,'active'=>0,'allowed'=>0,'country'=>'');
        Mage::getModel('country/category')->setData($data)->save();
    }
    //End
    $installer->endSetup();
?>

My problem is that I can't insert new values, so I tried to see whats inside the arrays/objects with this:

Mage::log(print_r($collection, TRUE), null, 'mylogfile1.log');

but without success, it doesn't even create the file

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Razorphyn_Country>
            <version>0.1.0</version>
        </Razorphyn_Country>
    </modules>

    <global>
        <models>
            <country>
                <class>Razorphyn_Country_Model</class>
                <resourceModel>country_mysql4</resourceModel>
            </country>

            <country_mysql4>
                <class>Razorphyn_Country_Model_Mysql4</class>
                <entities>
                    <product>
                        <table>razorphyn_country_product</table>
                    </product>
                    <category>
                        <table>razorphyn_country_category</table>
                    </category>                    
                </entities>
            </country_mysql4>
        </models>

        <resources>
            <country_setup>
                <setup>
                    <module>Razorphyn_Country</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </country_setup>
            <country_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </country_write>
            <country_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </country_read> 
        </resources>

        <blocks>
            <country>
                <class>Razorphyn_Country_Block</class>
            </country>
        </blocks>

        <helpers>
            <country>
                <class>Razorphyn_Country_Helper</class>
            </country>
        </helpers>
    </global>

    <adminhtml>
        <events>
            <catalog_product_save_after>
                <observers>
                    <razorphyn_country_catalog_product_save_after>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>saveProductAllowedCountry</method>
                    </razorphyn_country_catalog_product_save_after>
                </observers>
            </catalog_product_save_after>
        </events>

        <layout>
            <updates>
                <country>
                    <file>Razorphyn/country.xml</file>
                </country>
            </updates>
        </layout>
    </adminhtml>

    <frontend>
        <events>
            <!-- Retrieve Country as soon as possible Event -->
            <controller_front_init_before>
                <observers>
                    <razorphyn_country_controller_front_init_before>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>retrieveCountry</method>
                    </razorphyn_country_controller_front_init_before>
                </observers>
            </controller_front_init_before>
            <!-- Page Rendering event -->
            <core_block_abstract_to_html_after>
                <observers>
                    <razorphyn_country_core_block_abstract_to_html_after>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>checkMerchantable</method>
                    </razorphyn_country_core_block_abstract_to_html_after>
                </observers>
            </core_block_abstract_to_html_after>
            <!-- Add to cart Event -->
            <controller_action_predispatch_checkout_cart_add>
                <observers>
                    <razorphyn_country_controller_action_predispatch_checkout_cart_add>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>checkProductOnAdd</method>
                    </razorphyn_country_controller_action_predispatch_checkout_cart_add>
                </observers>
            </controller_action_predispatch_checkout_cart_add>

            <!-- Events related to potential address change -->
            <customer_address_save_after>
                <observers>
                    <razorphyn_country_customer_address_save_after>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>updateCartAddress</method>
                    </razorphyn_country_customer_address_save_after>
                </observers>
            </customer_address_save_after>

            <customer_login>
                <observers>
                    <razorphyn_country_customer_login>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>updateCartAddress</method>
                    </razorphyn_country_customer_login>
                </observers>
            </customer_login>

            <checkout_controller_onepage_save_shipping_method>
                <observers>
                    <razorphyn_country_checkout_controller_onepage_save_shipping_method>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>updateCartAddress</method>
                    </razorphyn_country_checkout_controller_onepage_save_shipping_method>
                </observers>
            </checkout_controller_onepage_save_shipping_method>


            <!-- Add js config -->
            <core_block_abstract_to_html_before>
                <observers>
                    <razorphyn_country_controller_action_predispatch_checkout_cart_add>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>addConfigJs</method>
                    </razorphyn_country_controller_action_predispatch_checkout_cart_add>
                </observers>
            </core_block_abstract_to_html_before>
        </events>

        <layout>
            <updates>
                <country>
                    <file>Razorphyn/country.xml</file>
                </country>
            </updates>
        </layout>
    </frontend>

</config>
7
  • Is logging enabled in your system configuration?
    – laketuna
    May 19, 2015 at 21:08
  • Yes, it's enabled
    – Razorphyn
    May 19, 2015 at 21:13
  • 2
    Place echo 'exiting'; die; at the beginning of your script and show us your <resources> node in config.xml.
    – laketuna
    May 19, 2015 at 21:17
  • added config.xml, I will add the code at the beginning of the php, is it necessary even if I know that the install is called (since the table are added and core_resource display my setup entry
    – Razorphyn
    May 19, 2015 at 21:23
  • If you know it runs for certain, it's not necessary. But I just saw this: Mage::getModel('country/product'). Your new model might not be set up correctly, and it may not be an issue with the installation script. I suggest you run an independent script to test your model saves.
    – laketuna
    May 19, 2015 at 21:26

1 Answer 1

3

Expanding on @musicliftsme's comment above, to enable logging in your Magento installation,

System > Configuration > Developer > Log Settings and set the Enabled option to Yes

Don't forget to set full access permissions (777) to the following folder: /var/log, since that is the place where those log files are saved.

Keep in mind

Mage::log('string to log'); will log to var/log/system.log

whereas

Mage::logException('string to log'); will log to var/log/exception.log

UPDATE: Also keep in mind none of the above will work in your install script. In order to have access to Mage::log() during an install script, you'll need to use a data install script. This should look like namespace/mymodule/data/data-install.1.0.0.php rather than namespace/mymodule/sql/install-1.0.0.php----UPDATE

To dump (output) some data to your screen during your installation script you can use die(); but first do:

echo '<pre>dumping... \n'; print_r($setup); echo '</pre>';
die();

If you do not see at least "dumping..." on your screen after refreshing, then your script did not run (check version in core_resource, etc.)

Another Suggestion:

Catch exceptions

try {
    $setup->saveConfig('razorphyn/country/buttons', json_encode(array()), 'default', 0);
} catch (Exception $e) {
    Mage::logException($e);
}
10
  • The problem was that exit() isn't correct, instead die works. However I still don't understand why it doesn't save the data, I will open another question...
    – Razorphyn
    May 20, 2015 at 6:59
  • It won't save the data because in your install.php script you don't have access to the Mage::log() function yet. If you want access to Mage::log() during an install script, you'll need to use a data install script, located in your data folder and named data-install-1.0.0.php rather than sql/install.php May 20, 2015 at 13:32
  • I was referring to the setData function to add some entries to the just created tables, ->setData()->save() doesn't add any entries to the tables, is it the same problem as you have just described?
    – Razorphyn
    May 20, 2015 at 14:13
  • I believe so... this: collection = Mage::getModel('catalog/product')->getCollection()->getAllIds(); will not work yet because you don't have access to Mage::getModel() in your install script. A data-install script would have access. Hope that helps. May 20, 2015 at 14:28
  • 1
    @TimHallman I would just like to point out that Mage::log() is available from within sql installer scripts with use of the $forceLog parameter. The reason it does not work without this is because sql installer scripts do not yet have access to store configuration data. Therefore Mage::getStoreConfig('dev/log/active') returns null, causing the Mage::log() method to think that logging is turned off (which we can override with the $forceLog parameter) Mar 21, 2016 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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