连接到自定义模块中的另一个数据库


Connect to another database in custom module

我在其config.xml文件中为umprodposts创建了一个新模块,并添加了以下xml。在控制器中,我添加了这个代码

$sql    = "SELECT * FROM comments";
$conn   = Mage::getSingleton('core/resource')->getConnection('forumprodposts_read');
$data   = $conn->fetchAll($sql);

但上面的代码抛出了一个错误,即注释不是核心数据库的表。我认为在我的xml中存在一些错误,因此没有建立到prodcomments数据库的连接。

<resources>
    <forumprodposts_write>
        <connection>
            <use>prodcomments_database</use>
        </connection>
    </forumprodposts_write>
    <forumprodposts_read>
        <connection>
            <use>prodcomments_database</use>
        </connection>
    </forumprodposts_read>
    <forumprodposts_setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </forumprodposts_setup>
    <forumprodposts_database>
        <connection>
            <host><![CDATA[localhost]]></host>
            <username><![CDATA[username]]></username>
            <password><![CDATA[password]]></password>
            <dbname><![CDATA[prodcomments]]></dbname>
            <model>mysql4</model>
            <type>pdo_mysql</type>
            <active>1</active>
        </connection>
    </forumprodposts_database>
</resources>

您将数据库资源命名为<forumprodposts_database>,但告诉您的读/写句柄使用prodcomments_database作为资源。从更改<use>节点

<use>prodcomments_database</use>

<use>forumprodposts_database</use>

应该解决这个问题。