Home » Php » php – My Magento Extension Install Script Will Not Run

php – My Magento Extension Install Script Will Not Run

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to create an install script for my extension and for some reason it will not the install script. The extension will show up in the core_resource table, but the attributes I am trying to create will not create.

I am pretty sure that the script is not even being called because I put an exit() at the beginning and the site ran just fine.

Here is what I have in my config XML file. This is placed inside global -> resources path:

<nie_setup>
    <setup>
        <module>Nie_Nie</module>
    </setup>
    <connection>
        <use>core_setup</use>
    </connection>
</nie_setup>

My install script is as follows:

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$setup->addAttribute('customer', 'nie_admin', array(
    'input'                 => 'text',
    'type'                  => 'text',
    'backend'               => '',
    'visible'               => 0,
    'required'          => 0,
    'user_defined'  => 1,
));

$installer->endSetup();

Is there something obvious I am missing here that would be the reason the script will not run?

How to&Answers:

Work your way through this article to make sure you don’t have any misunderstanding of what the setup resources do, how they work, and how you can troubleshoot them.

Once you’ve done that, from everything you’ve said on this question thread it sounds like you’re getting your resource “installed”, but that your install script never runs. My guess is that the version number you used in

//0.0.1 is your version number
mysql4-install-0.0.1.php

didn’t match up with the version of your module

<modules>
    <Nie_Nie>
        <version>?.?.?</version>
    </Nie_Nie>
</modules>

Those should match for the script to run. I think Magento is smart enough to run previous versions if it finds them, but the code in the setup resources is the kind that’s hard to follow, so I always make sure they match.

Regardless, here’s how you can see which file(s) magento is trying to run when it runs your setup resource. Delete any entries from core_resource related to your module. Clear your cache. Then find the following locations in the setup class

app/code/core/Mage/Core/Model/Resource/Setup.php:

protected function _modifyResourceDb($actionType, $fromVersion, $toVersion)
{
    ... 

    $sqlFilesDir = Mage::getModuleDir('sql', $modName).DS.$this->_resourceName;        

    if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
        return false;
    }

    ...

    $sqlDir->close();

    if (empty($arrAvailableFiles)) {
        return false;
    }

    ...

    $arrModifyFiles = $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrAvailableFiles);
    if (empty($arrModifyFiles)) {
        return false;
    }

and then modify them to add some temporary debugging exceptions

    if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
        throw new Exception("$sqlFilesDir not found");
        return false;
    }

    ...

    if (empty($arrAvailableFiles)) {
        throw new Exception("No files found to run");
        return false;
    }

    ...

    $arrModifyFiles = $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrAvailableFiles);
    if (empty($arrModifyFiles)) {
        throw new Exception("No valid upgrade files found to run for ");
        return false;
    }

    throw new Exception("If you're getting here, we have a file.  Remove your exceptions here and place one in your installer to make sure it's the one you think it is.");

Reload the page and you’ll get exception text complaining about whatever Magento can’t find. That should be enough to help you track down which installer script Magento is trying to run, but failing to find. Just remember to delete your module’s row in core_resource and to clear your cache. (Magento caches which modules need to check for an install/upgrade)

If that doesn’t work, start digging into the logic of applyAllDataUpdates and figure out why the class isn’t including your installer file.

Answer:

When I encountered this problem I acually had to disable the cache. Merely flushing it did not help for whatever reason.

Answer:

The easiest and most informative way to track down this error is to setup your IDE to debug Magento and set a breakpoint in your mysql4-install-0.0.1.php. If the breakpoint doesn’t get hit, then you know if the issue is in your XML config. If the breakpoint does get hit, you can trace through the code to find the source of the error.

It may take you half a day to setup, but live debugging of Magento is by far the best way to learn and understand the code. Do yourself a favour, make the investment now.

Answer:

As per Magento Knowledgebase you could try including a <class> tag in your <setup>. This way you can ensure the correct setup model is used and (if it gets that far) passes the model to your install script negating the need to create a $setup manually.

Check the file permissions of the install script and the directory it is in. I sometimes find deleting the record from core_resources helps kick start the process too.

Answer:

You can check in Magento whcih modules are loaded and which version of that module is loaded:

  1. Go to app/code/core/Mage/Core/Model/Resource/Setup.php
  2. Go to function __construct()
  3. At the end of function write:

    Mage::log($modName);
    Mage::log($this->_moduleConfig);

It will log all the modules loaded with there version number. Here you can check if your module is loaded or not.

Answer:

You should change the version of your module one point up, to make your update script execute.

<modules>
    <Nie_Nie>
        <version>1.5.0.0</version>
    </Nie_Nie>
</modules>

If this version is equals to the resource version from core_resources table upgrade script will not execute. And the version should match the name of your upgrade script

Answer:

We had the same problem for our store http://www.looxis.de
To update an extension that we have in use we transfered all files via FTP, but the database wouldn’t update itself after clearing the cache. So the updated extension failed to run, we couldn’t login into the backend.

Searching for a solution we found this page.

The problem was, that we installed a newer version of the module a few weeks before, which also produced errors due to a conflict with other modules, so we transferred back partially from our database backup some tables and we also put back the old files. everything was working again,

when we tried to newly update the module, which was now compatible with the other extension (conflict was removed) the sql update script wouldn’t run.

This was due to the table “core_resources.” in there, the module version number was set to the newest version that we had installed weeks before – so magento wouldn’t recognise that a new update had been performed again, it assumed the newest version was already there.

We manually changed the version number to a lower version, and boom, the upgrade script initiated and everything was working fine!

Answer:

Make sure to check your app/etc/modules file, make sure the name of your module is accurate and that the codepool is accurately specified.