php 如何获取magento表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15137959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:38:52  来源:igfitidea点击:

how to get magento table name

phpsqlmagentomoduleinstall

提问by Benubird

I have this global config:

我有这个全局配置:

<global>
    <models>
    <subscriber>
        <class>Giftlab_Subscriber_Model</class>
        <resourceModel>subscriber_resource</resourceModel>
    </subscriber>
    <subscriber_resource>
        <class>Giftlab_Subscriber_Model_Resource</class>
        <entities>
            <records>
                <table>subscriber_records</table>
            </records>
        </entities>
    </subscriber_resource>
    </models>
    <resources>
        <giftlab_subscriber_write>
            <connection>
                <use>core_write</use>
            </connection>
        </giftlab_subscriber_write>
        <giftlab_subscriber_read>
            <connection>
                <use>core_read</use>
            </connection>
        </giftlab_subscriber_read>
        <giftlab_subscriber_setup>
            <setup>
                <module>Giftlab_Subscriber</module>
                <class>Giftlab_Subscriber_Model_Resource_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </giftlab_subscriber_setup>
    </resources>
</global>

in my mysql4-install-0.1.0.php file, I need to get the table name. How do I do it? I know it is something like this:

在我的 mysql4-install-0.1.0.php 文件中,我需要获取表名。我该怎么做?我知道它是这样的:

$this->getTable('subscriber_resource/records')

But that only produces an exception Can't retrieve entity config: subscriber_resource/records. What do I need to do to retrieve the table name?

但这只会产生异常Can't retrieve entity config: subscriber_resource/records。我需要做什么来检索表名?

回答by Benubird

Figured out the answer myself, although thanks to @Yaroslav for directing my to Alan Storm's tutorial, as that helped.

我自己想出了答案,尽管感谢 @Yaroslav 将我引导到 Alan Storm 的教程,因为这有所帮助。

The answer is, I need this:

答案是,我需要这个:

$this->getTable('subscriber/records');

where "subscriber" is the name of the config entry for the model (not the resource), and "records" is the entity. Turns out that when magento parses thinga/thingb, it always assumes that thinga before the slash is the model and dereferences that to get the resource, by looking in the config for <thinga><resourceModel>{resourcemodel}, and then looking again for <{resourcemodel}><entities><thingb><table>to get the table name.

其中“订阅者”是模型(不是资源)的配置条目的名称,“记录”是实体。事实证明,当 magento 解析 thinga/thingb 时,它总是假设斜杠之前的 thinga 是模型并取消引用它以获取资源,通过在配置中查找<thinga><resourceModel>{resourcemodel},然后再次查找<{resourcemodel}><entities><thingb><table>以获取表名。

So my config was correct, I was just getting confused between the idea of model and resource. Hope this helps someone else getting stuck on the same problem - I couldn't find any clear info on this in the various tutorials.

所以我的配置是正确的,我只是在模型和资源的概念之间感到困惑。希望这可以帮助其他人陷入同样的​​问题 - 我在各种教程中找不到任何明确的信息。

回答by Yaroslav

On Magento you work with Collections. See a sample:

在 Magento 上,您使用Collections. 查看示例:

$mysubscriber_recordsCollection = Mage::getModel('records/subscriber_records')->getCollection()

Check useful Alan Storm site, specially the tutorial on Magento models.

查看有用的 Alan Storm 站点,特别是有关 Magento 模型教程

EDIT

编辑

To get table names on the setup install here is a detailed sample. Pay attention to the $installer->getTable('records/subscriber_records')here is where table name is loaded.

要在安装程序中获取表名,这里是一个详细的示例。注意$installer->getTable('records/subscriber_records')这里是加载表名的地方。

<?php
    echo 'Running This Upgrade: '.get_class($this)."\n <br /> \n";
    $installer = $this;
    $installer->startSetup();
    $installer->run("
        CREATE TABLE `{$installer->getTable('records/subscriber_records')}` (
            `subscriber_records_id` int NOT NULL AUTO_INCREMENT,
            PRIMARY KEY (`subscriber_records_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    ");
    $installer->endSetup();

For the setup resources check this other Alan Storm's tutorial

对于设置资源,请查看其他Alan Storm 的教程

回答by naitsirch

If you want to get the table name of a resource that does nothave a resource model (like catalog/category_product) and you are notin a setup script, you can do it this way:

如果您想获取没有资源模型(如catalog/category_product)并且您不在安装脚本中的资源的表名,您可以这样做:

$table = Mage::getSingleton('core/resource')->getTableName('catalog/category_product');

This is helpful for all N:M relations where you usually does not need a model class.

这对于通常不需要模型类的所有 N:M 关系都有帮助。