Connecting Module to External Database fails to execute Resource
-
12-12-2019 - |
Question
I have followed http://www.solvingmagento.com/accessing-an-external-database-from-your-magento-module/ and articles by Alan Storm and am stuck on connecting a Magento Module to an external database. [Mage/Core version in 1.6 on this project].
I think the configuration appears correct, but I can't get data to the screen. Instead, actual php code is being displayed. Specifically, the whole Resource class is being displayed.
Here is the relevant controller, model, model resource, and config snippet. I am not sure of the Resource model which should be extended, that may be the problem, but maybe there is something else too?
My Controller
public function indexAction()
{
echo "<h1> Connecting Magento to Developer Store! </h1>";
$externalDataRead = Mage::getModel('developer_integration/customer');
$externalDataRead->load(1, 'user_id');
echo '<pre>';
print_r($externalDataRead->getData());
echo '</pre>';
}
My Model
class Developer_Integration_Model_Customer extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('developer_integration/customer');
}
}
My Model Resource
class Developer_Integration_Model_Resource_Customer extends Mage_Core_Model_Resource_Type_Db_Pdo_Mysql
{
protected function _construct()
{
$this->_init('developer_integration/customer', 'user_id');
}
}
My config.xml (this may not appear well-formatted here, but is in file)
<models>
<developer_integration><!-- name of the model configuration-->
<class>Developer_Integration_Model</class><!-- path where to look for model classes for-->
<resourceModel>developer_integration_resource</resourceModel><!-- pointer to the resource model configuration node-->
</developer_integration>
<developer_integration_resource><!-- resource model configuration name -->
<class>Developer_Integration_Model_Resource</class><!-- path where to look for resource model classes for -->
<entities><!-- entities list -->
<customer><!-- our test entity -->
<table>dev_customer</table><!-- name of the external table to store data for our entity -->
</customer>
</entities>
</developer_integration_resource>
</models>
<resources>
<developer_integration_setup><!-- name of the external db connection -->
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[devuser]]></username>
<dbname><![CDATA[dev_store]]></dbname>
<type><![CDATA[pdo_mysql]]></type>
<active>1</active>
</connection>
</developer_integration_setup>
<developer_integration_write><!-- connection used for write access -->
<connection>
<use>developer_integration_setup</use>
</connection>
</developer_integration_write>
<developer_integration_read><!-- connection used for read access -->
<connection>
<use>developer_integration_setup</use>
</connection>
</developer_integration_read>
</resources>
Solution 2
Well, the answer to this is to keep breaking in the model and resource until you can trigger the blue magento error screen or something like it - so that you can tell that the errors you are hitting are inside the Resource.
In my case, I found that making my Resource extend class Mage_Core_Model_Resource_Db_Abstract
triggered an execution error. While that had seemed to be a sign that my Resource was extending the wrong Resource Model, it was actually a sign that I was extending the correct Resource Model.
It is worth noting that the Magento api docs at http://docs.magentocommerce.com/index.html do not list the Mage_Core_Model_Resource_Db_Abstract
. The Mage_Core_Model_Resource_Abstract is listed, as is Mage_Core_Model_Resource, but Mage_Core_Model_Resource_Db_Abstract
is not. The only reason I was willing to retry that class was because it was listed in the solvingmagento.com tutorial. I could have also grep'ed for the class within my installation and found that it existed.
So, that atleast leads to the issue, compounded by the above api absence... sometime in the midst of my hacking around, I removed the <password>
node from my connection because my local dev password is empty. Nevertheless, when the xml is parsed by Magento, is appears esential to have that field present. It turns into <password />
within the connection configuration and is required [Another point missing from the magento configuration wiki].
If anyone else has this problem - maybe this information will cut down on time in the weeds. Best to all.
OTHER TIPS
Replace the Models and Resource tags with this:
<models>
<developer_integration><!-- name of the model configuration-->
<class>Developer_Integration_Model</class><!-- path where to look for model classes for-->
<resourceModel>developer_integration_mysql4</resourceModel><!-- pointer to the resource model configuration node-->
</developer_integration>
<developer_integration_mysql4><!-- resource model configuration name -->
<class>Developer_Integration_Model_Mysql4</class><!-- path where to look for resource model classes for -->
<entities><!-- entities list -->
<customer><!-- our test entity -->
<table>dev_customer</table><!-- name of the external table to store data for our entity -->
</customer>
</entities>
</developer_integration_mysql4>
</models>
<resources>
<developer_integration_setup><!-- name of the external db connection -->
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[devuser]]></username>
<dbname><![CDATA[dev_store]]></dbname>
<type><![CDATA[pdo_mysql]]></type>
<active>1</active>
</connection>
</developer_integration_setup>
<developer_integration_write><!-- connection used for write access -->
<connection>
<use>core_write</use>
</connection>
</developer_integration_write>
<developer_integration_read><!-- connection used for read access -->
<connection>
<use>core_read</use>
</connection>
</developer_integration_read>
</resources>