When creating a custom module in Magento it is sometimes required to create custom attributes in the system. Typically, I would extend the relevant module with my setup model or simply extend Mage_Core_Model_Resource_Setup.
If my module requires to add attributes to specific models and requires specific setup models i.e. eav or sales - then what is the best practice here. I am thinking to define my own setup model: MyCompany_MyModuel_Reseource_Setup (for 1.6 +) and then in my sql install file just create the required setup models. Is this the best approach here?
It's entirely acceptable (and appropriate) to use other module setup files in your module's files:
$otherSetup = Mage::getResourceModel('catalog/setup','default_setup');
$otherSetup->addAttribute(...);
Especially in the case of installing new attributes, there are entity-specific attribute defaults which each module will handle for you (see _prepareValues()
methods).
This will of course be executed in your module's setup class instance, but that's a-ok.
EDIT: I've re-read your question, and I'm not sure if I entirely covered off what you're asking. Nevertheless the information applies somewhat so I'll leave it here. However I think your question is regarding best practice for adding attributes to existing entities. Specifically on that point, It's perfectly acceptable to use setup resources to add attributes to things, EAV or otherwise. You can use functions like Mage_Eav_Model_Entity_Setup::addAttribute()
to this end, or use Mage_Core_Model_Resource_Setup::run()
to modify tables with SQL queries as required.
Original answer:
It's possible to use multiple resources within the same module, so this should be sufficient to do what you're asking.
It's pretty straightforward in fact, just define two setup resources and Magento will run both.
in your config.xml:
<config>
...
<global>
...
<resources>
<!-- Resource 1 -->
<mymodule_setup>
<setup>
<module>MyCompany_MyModule</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
<mymodule_write>
<connection>
<use>core_write</use>
</connection>
</mymodule_write>
<mymodule_read>
<connection>
<use>core_read</use>
</connection>
</mymodule_read>
<!-- Resource 2 -->
<mymodule2_setup>
<setup>
<module>MyCompany_MyModule</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule2_setup>
<mymodule2_write>
<connection>
<use>core_write</use>
</connection>
</mymodule2_write>
<mymodule2_read>
<connection>
<use>core_read</use>
</connection>
</mymodule2_read>
</resources>
...
</global>
...
</config>
Then include the necessary install/upgrade files in app/code/local/MyCompany/MyModule/sql/mymodule_setup
and app/code/local/MyCompany/MyModule/sql/mymodule2_setup
You can even add your own functions to the installer by extending the setup model. You do this by adding a class tag to:
<resources>
<mymodule2_setup>
<setup>
like this:
<class>MyCompany_MyModule_Entity_Setup</class>
and create a file at app/code/local/MyCompany/MyModule/Entity/Setup.php
that looks like this:
<?php
class MyCompany_MyModule_Entity_Setup extends Mage_Eav_Model_Entity_Setup
{
}
So as you suspected, you're going to need to extend Mage_Core_Model_Resource_Setup or Mage_Eav_Model_Entity_Setup (eav).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With