2

I have 2 modules: ModuleA and ModuleB.

  • ModuleA creates some categories in a data install script.
  • ModuleB creates a new category attribute in a data install script.

(the modules do other things, but these are the important things to mention for this question)

ModuleA sets data on the categories when created and I want it to set data for the attribute created in ModuleB.

But the values never persist if both modules are installed at the same time.

I currently have ModuleA depending on ModuleB but that makes no difference.

How can i resolve this issue and have both moudles installable at the same time?


EDIT TO QUESTION:

I have now moved the code for populating the category values into the same module and exactly same setup script that installs the attribute. It also fails.

Therefore it would appear that it is not possible to create a new category attribute and populate values for that attribute during the same setup.

Is this a bug, or correct behaviour?

9
  • Magento does not install two modules on same time. First you should install B, after that install A. Modules cannot be interdepend.
    – mageUz
    Commented Jan 22, 2014 at 18:37
  • But if 2 modules are added to the codebase at the same time each modules setup will be run. It is during this that I am referring to. Commented Jan 22, 2014 at 18:38
  • "I currently have ModuleA depending on ModuleB but that makes no difference." — Why not? Are you saying that you've set your dependency and the modules still install in the wrong order? Or as you saying then install in the right order but the second module can't read the information it needs to? Commented Jan 22, 2014 at 19:09
  • I can confirm that they are definitely run in the correct order. The problem is that ModuleA when creating the categories and trying to set data for the new attribute created by ModuleB, the value will not persist. Commented Jan 22, 2014 at 19:13
  • If i comment out the resources config node for ModuleA and install everything. then uncomment it and let its setup script run then the values persist nicely. Its like there is something magento does when creating a new attribute during setup scripts running that requires for the whole process to complete before the values can be set again during a new setup script Commented Jan 22, 2014 at 19:19

3 Answers 3

2

You mention "data install script". Are you referring to Magento's "data" update scripts or the standard "sql" install scripts?

In your example moduleB should be using the standard "sql" convention:

Note: The setup resource class that I've used in the past is Mage_Catalog_Model_Resource_Setup.

    <global>
    ...
       <resources>
          <mynamespace_moduleb_setup>
            <setup>
              <module>MyNameSpace_ModuleB</module>
              <class>Mage_Catalog_Model_Resource_Setup</class>
            </setup>
          </mynamespace_moduleb_setup>
       </resources>
    ...
    </global>

app/code/local/MyNameSpace/ModuleB/sql/install-1.0.0.php

<?php
/* @var $this Mage_Catalog_Model_Resource_Setup */
//add in category attributes
$categoryEntityId = $this->getEntityTypeId('catalog_category');
$this->addAttribute($categoryEntityId, 'cool_new_attribute', array(
    'type'          => 'varchar',
    'input'         => 'text',
    'label'         => 'Cool New Attribute',
    'required'      => '0',
    'user_defined'  => '1'
));
$attributeId    = $this->getAttributeId($categoryEntityId, 'cool_new_attribute');
$setId          = $this->getDefaultAttributeSetId($categoryEntityId);
$groupId        = $this->getAttributeGroupId($categoryEntityId, $setId, 'General Information');
$this->addAttributeToSet($categoryEntityId, $setId, $groupId, $attributeId);

Now in moduleA, note the data path and prefix:

app/code/local/MyNameSpace/ModuleB/data/data-install-1.0.0.php

$defaultCategory = Mage::getModel('catalog/category')->load(Mage_Catalog_Model_Category::TREE_ROOT_ID)->getChildrenCategories()->getFirstItem();
$category = Mage::getModel('catalog/category');
$category->setData(array(
    'name'        => 'My New Category',
    'is_active'   => true,
    'description' => 'New Category',
    'meta_title'  => 'New Category',
    'url_key'     => 'new-category',
    'is_anchor'   => true,
    'path'        => $defaultCategory->getPath(),
    'cool_new_attribute' => 'Cool new value',
    'include_in_menu' => $incMenu
))->save();

I tested this quickly in a 1.8 instance and it worked. Good luck!

0

Most likely you've just hit the bowels of Magento's ORM and transactions.

A code snippet or two would help expedite a proper approach to take.

As with most of Magento, the DB layer is built on lasagna stacks as well :)

However consider the following code:

try {
    $conn->beginTransaction();
    $objectOne->save();
    $objectTwo->save();
    $conn->commit();
} catch( Exception $e ) {
    $conn->rollback();
}

Even though ->save() has been called it is not really committed to the DB yet, ->commit()is needed to push fully to the db, this is why sometimes values will not exist during same stack runtime until after a full complete stack load was completed. When a final commit of queued transactions is made. (if not specified to by your module)

-2

If 2 modules added to codebase at the same time, Magento does not install them parallel, all module configuration file (app/etc/modules/*.xml) are loaded ordering by file name ascending. And wich module configuration file comes first than this module will be installed first. If you want ModuleB is to be installed before ModuleA try to give proper name. For example Module_AB (same with ModuleB) and Module_BA (same as ModuleA).

Updates

I tested that we can manage module loading using <depends/> tag. Thanks for @beeplogic to guide me right derection. @MartyWallace, during my research on your question I faced following probleblem, maybe your issues also same with it: Category update during module installation

1
  • It's possible to coerce magento to load modules in a particular order by using the <depends/> tag in the definition XML.
    – beeplogic
    Commented Jan 23, 2014 at 5:24

Your Answer

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

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