简体   繁体   English

如何在Magento模型中覆盖表名和前缀?

[英]How to override table name andprefix in Magento model?

I need to make Magento read/write data to an existing database table in order to integrate with another party system. 我需要使Magento读取/写入现有数据库表中的数据,以便与另一方系统集成。 The table cannot be renamed. 该表无法重命名。 If I specify it in the table tag in the model config as below magento adds the prefix. 如果我在模型配置的table标记中指定它,如下所示,magento将添加前缀。 Is there a way to override the prefixing somehow? 有没有办法以某种方式覆盖前缀?

   <models>
        <arithmetic>
            <class>Mcmr_Arithmetic_Model</class>
            <resourceModel>arithmetic_mysql4</resourceModel>
        </arithmetic>
        <arithmetic_mysql4>
            <class>Mcmr_Arithmetic_Model_Mysql4</class>
            <entities>
                <arithmetic>
                    <table>newslettersignups</table>
                </arithmetic>
            </entities>
        </arithmetic_mysql4>
    </models>

There is not an easy way to do that. 没有一个简单的方法可以做到这一点。

The best solution would be to extend the configuration file : 最好的解决方案是扩展配置文件:

<models>
    <arithmetic>
        <class>Mcmr_Arithmetic_Model</class>
        <resourceModel>arithmetic_mysql4</resourceModel>
    </arithmetic>
    <arithmetic_mysql4>
        <class>Mcmr_Arithmetic_Model_Mysql4</class>
        <entities>
            <arithmetic>
                <ignore_prefix>1</ignore_prefix>
                <table>newslettersignups</table>
            </arithmetic>
        </entities>
    </arithmetic_mysql4>
</models>

Then you could override the getTableName method of Mage_Core_Model_Resource class to make it consider the additional config attribute "ignore_prefix" for the entity. 然后,您可以覆盖Mage_Core_Model_Resource类的getTableName方法,以使其考虑实体的附加配置属性“ ignore_prefix”。

if ($mappedTableName) {
    $tableName = $mappedTableName;
} else {
    if($entityConfig->ignore_prefix)
        $tablePrefix = '';
    else
        $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
    $tableName = $tablePrefix . $tableName;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM