Magento - possible to have multiple tables for a single model?

淺唱寂寞╮ 提交于 2019-12-10 14:23:44

问题


trying to set up a config file for a custom module - do I need to have a unique model for each 'resourceModel', or is it possible to have multiple table entities per model?

Is it possible to get something like this to work:


    <config>...
 <model>
  <namespace>
    <class>Namespace_Module_Model</class>
    <resourceModel>module_mysq4</resourceModel>
  </namespace>
  <module_mysql4>
   <class>Namespace_Module_Model_Mysql4</class>
    <entities>
        <table_1>
            <table>table_1</table>
        </table_1>
        <table_2>
            <table>table_2</table>
        </table_2>
        <table_3>
            <table>table_3</table>
        </table_3>
        ...
    </entities>
   </module_mysql4>
..</config>

and then dynamically switch between the tables through the model?

and related: Anyone know what the possible children of the are and their properites? I've seen 'entities', 'associations' and 'items' - thx


回答1:


It's not really clear what you're asking here. Magento has a basic one resource to one table resource, and a one resource to many tables constructed in a specific manner resource for EAV style models.

The scenario you're describing above isn't directly supported by the system, but if you wanted to implement something like it there's nothing stopping you from implementing a resource that works any way you want.

As for the possible children, create the simple config viewer described here to get a dump of the entire merged config, and then use an xpath viewer to examine all the nodes (and their children) that you're interested in




回答2:


Thx for the response & apologies if the question was unclear. After a few hours of debugging, I have it working with the following structure:


<models>
  <modulename>
   <class>Namespace_Modulename_Model</class>
   <resourceModel>modulename_mysql4</resourceModel>
  </modulename>
  <modulename_type1>
   <class>Namespace_Modulename_Model_Type1</class>
   <resourceModel>modulename_mysql4</resourceModel>
  </modulename_type1>
   <modulename_type2>
   <class>Namespace_Modulename_Model_Type2</class>
   <resourceModel>modulename_mysql4</resourceModel>
  </modulename_type2>
  <modulename_mysql4>
   <class>Namespace_Modulename_Model_Mysql4</class>
   <entities>
    <modulename>
     <table>modulename</table>
    </modulename>
     <modulename_type1>
     <table>modulename_type1</table>
    </modulename_type1>
     <modulename__type2>
     <table>modulename_type2</table>
    </modulename_type2>
   </entities>
  </modulename_mysql4>

 </models>

So yes - there is a single table entity for each model declared (one model, one resource) but I would have assumed that each additional model/resourceModel combination would require it's own separate Model_Mysql class in it's own modulename_mysql4 node, ala:


<models>
 <modulename>
  <class>Namespace_Modulename_Model</class>
  <resourceModel>modulename_mysql4</resourceModel>
 </modulename>
 <modulename_type1>
  <class>Namespace_Modulename_Model_Type1</class>
  <resourceModel>modulename_mysql4_type1</resourceModel>
 </modulename_type1>
  <modulename_type2>
  <class>Namespace_Modulename_Model_Type2</class>
  <resourceModel>modulename_mysql4_type2</resourceModel>
 </modulename_type2>
 <modulename_mysql4>
  <class>Namespace_Modulename_Model_Mysql4</class>
  <entities>
   <modulename>
    <table>modulename</table>
   </modulename>
  </entities>
 </modulename_mysql4>
 <modulename_mysql4_type1>
  <class>Namespace_Modulename_Model_Mysql4_Type1</class>
  <entities>
    <modulename_type1>
    <table>modulename_type1</table>
   </modulename_type1>
  </entities>
 </modulename_mysql4_type1>
 <modulename_mysql4_type2>
  <class>Namespace_Modulename_Model_Mysql_Type2</class>
  <entities>
    <modulename_type2>
    <table>modulename_type2</table>
   </modulename_type2>
  </entities>
 </modulename_mysql4_type2>
</models>

but that is not the case. Would love to hear a play by play explanation. Thx for the help!




回答3:


Or:

    <resources>
        <modulename_setup>
            <setup>
                <module>modulename</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </modulename_setup>
        <modulename_write>
            <connection>
                <use>core_write</use>
            </connection>
        </modulename_write>

        <modulename_read>
            <connection>
                <use>core_read</use>
            </connection>
        </modulename_read>
    </resources>


来源:https://stackoverflow.com/questions/4349058/magento-possible-to-have-multiple-tables-for-a-single-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!