เป็นไปได้หรือไม่ที่จะเชื่อมต่อกับฐานข้อมูลอื่นจาก Magento และการเข้าถึงข้อมูล?
หากฉันต้องการสร้างโมดูลฉันจะสร้างโมดูลเพื่อเข้าถึงฐานข้อมูลอื่นได้อย่างไร มีการสอนใดบ้างที่บอกเกี่ยวกับสิ่งเดียวกันตั้งแต่เริ่มต้น? ความคิดใด ๆ
เป็นไปได้หรือไม่ที่จะเชื่อมต่อกับฐานข้อมูลอื่นจาก Magento และการเข้าถึงข้อมูล?
หากฉันต้องการสร้างโมดูลฉันจะสร้างโมดูลเพื่อเข้าถึงฐานข้อมูลอื่นได้อย่างไร มีการสอนใดบ้างที่บอกเกี่ยวกับสิ่งเดียวกันตั้งแต่เริ่มต้น? ความคิดใด ๆ
คำตอบ:
สิ่งแรกที่คุณต้องทำคือสร้างการเชื่อมต่อในโมดูล config.xml ของคุณ มันควรจะมีลักษณะคล้ายกับในของคุณdefault_setup /app/etc/local.xmlที่นี่คุณสามารถระบุโฮสต์ให้เป็น localhost จากนั้นตั้งค่า dbname อื่นหรือคุณสามารถระบุโฮสต์อื่นโดยสมบูรณ์ ฉันยังใช้ซ็อกเก็ตก่อนซึ่งก็ใช้ได้เช่นกัน
<resources>
<new_db>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[db_username]]></username>
<password><![CDATA[db_password]]></password>
<dbname><![CDATA[db_name]]></dbname>
<model>mysql4</model>
<type>pdo_mysql</type>
<active>1</active>
</connection>
</new_db>
</resources>
หลังจากนี้คุณจะสามารถเชื่อมต่อกับฐานข้อมูลนี้และดำเนินการแบบสอบถามดังนี้
$new_db_resource = Mage::getSingleton('core/resource');
$connection = $new_db_resource->getConnection('new_db');
$results = $connection->query('SELECT * FROM table');
หากคุณต้องการที่จะทำนี้ผ่านทางรูปแบบแล้วคุณสามารถระบุread, writeและsetupทรัพยากรดังต่อไปนี้ สิ่งนี้จะทำภายในresourcesโหนดใน config.xml ของคุณอีกครั้งและคุณควรแทนที่testด้วยโมเดลของคุณที่ตั้งค่าไว้
<resources>
<new_db>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[db_username]]></username>
<password><![CDATA[db_password]]></password>
<dbname><![CDATA[db_name]]></dbname>
<model>mysql4</model>
<type>pdo_mysql</type>
<active>1</active>
</connection>
</new_db>
<test_write>
<connection>
<use>new_db</use>
</connection>
</test_write>
<test_read>
<connection>
<use>new_db</use>
</connection>
</test_read>
<test_setup>
<connection>
<use>new_db</use>
</connection>
</test_setup>
</resources>
<models>
<test>
<class>My_Test_Model</class>
<resourceModel>test_resource</resourceModel>
</test>
<test_resource>
<class>My_Test_Model_Resource</class>
<entities>
<test>
<table>test</table>
</test>
</entities>
</test_resource>
</models>
getConnection /app/code/core/Mage/Core/Model/Resource.phpรูปแบบที่ตัวเองจะพยายามหาข้อมูลการเชื่อมต่อของมันในการทำงาน หากคุณเข้าสู่ระบบ$nameผ่านคุณจะเห็นค่าเช่นpoll_write, tag_writeและcms_readที่ส่วนแรกตรงกับส่วนรูปแบบใน config.xml ในกรณีของเราคุณจะเห็นtest_write, หรือtest_read test_setupหากไม่สามารถพบการเชื่อมต่อการจับคู่นี้แล้วมันจะใช้การเชื่อมต่อเริ่มต้นcore_read, core_writeหรือcore_setup
หลังจากอ่านคำตอบเหล่านี้แล้วการค้นหาและทำแบบทดสอบฉันพบวิธีนี้ นี่คือบล็อกของฉันที่ฉันเขียนวิธีการแก้ปัญหา
ทำงานกับ Magento 1.9 ฉันถูกขอให้ทำการเชื่อมต่ออ่านและเขียนหลายครั้ง Magento มีความเป็นไปได้ที่จะกำหนดค่าการเชื่อมต่อแบบอ่านและเขียนใน /etc/local.xml เพียงตั้งค่าการใช้แท็กเพื่อให้ Magento ทราบว่ามีรายการใดบ้าง
<default_setup>
<connection>
<!-- LOCALHOST -->
<host>localhost</host>
<username>root</username>
<password>123456</password>
<dbname>magento_db</dbname>
<initstatements>SET NAMES utf8</initstatements>
<model>mysql4</model>
<type>pdo_mysql</type>
<pdotype></pdotype>
<active>1</active>
</connection>
</default_setup>
<default_read>
<connection>
<use/>
<!-- ANOTHER SERVER -->
<host>other_server</host>
<username>root</username>
<password>123456</password>
<dbname>magento_db</dbname>
<initstatements>SET NAMES utf8</initstatements>
<model>mysql4</model>
<type>pdo_mysql</type>
<pdotype></pdotype>
<active>1</active>
</use></connection>
</default_read>
<default_write>
<connection>
<use/>
<!-- LOCALHOST -->
<host>localhost</host>
<username>root</username>
<password>123456</password>
<dbname>magento_db</dbname>
<initstatements>SET NAMES utf8</initstatements>
<model>mysql4</model>
<type>pdo_mysql</type>
<pdotype></pdotype>
<active>1</active>
</use></connection>
</default_write>
เราสามารถกำหนดการเชื่อมต่อ n ในไฟล์ config เดียวกันเช่นตัวอย่างการทดสอบนี้
<test_read>
<connection>
<!-- TEST SERVER -->
<host>test_server</host>
<username>root</username>
<password>123456</password>
<dbname>magento_db</dbname>
<initstatements>SET NAMES utf8</initstatements>
<model>mysql4</model>
<type>pdo_mysql</type>
<pdotype></pdotype>
<active>1</active>
</connection>
</test_read>
ขีด จำกัด คือการเชื่อมต่อจะใช้กับทั้งระบบ แต่ความคิดของฉันคือการตั้งค่าสำหรับทรัพยากรบางอย่างเท่านั้น ในกรณีนี้ฉันมีโมดูลรายงานที่กำหนดเองซึ่งฉันเพียงต้องการทำการเชื่อมต่อแบบอ่านในตารางคำสั่งซื้อ หลังจากเอาชนะทรัพยากรการสั่งซื้อผู้วิเศษ / การขาย / รุ่น / ทรัพยากร / Order.php เพียงแค่ทำการปรับปรุง 3
//ธง
สาธารณะ $ reportConnection = false;
/ **
* เพียงเพิ่มการเชื่อมต่อที่กำหนดใน local.xml 'test_read'
* /
ฟังก์ชันที่มีการป้องกัน _construct () {
$ this -> _ init ('ยอดขาย / คำสั่งซื้อ', 'เอนทิตี _id');
$ this -> _ resources-> getConnection ( 'test_read');
}
/ **
* ทำการเชื่อมต่อหากตั้งค่าสถานะ
* /
ฟังก์ชันที่มีการป้องกัน _getConnection ($ connectionName) {
if (isset ($ this -> _ connections [$ connectionName])) {
ส่งคืน $ this -> _ การเชื่อมต่อ [$ connectionName];
}
if ($ connectionName == 'read' && $ this-> reportConnection)
$ this -> _ connections [$ connectionName] = $ this -> _ resources-> getConnection ('test_read');
อื่น{
if (! empty ($ this -> _ resourcePrefix)) {
$ this -> _ connections [$ connectionName] = $ this -> _ resources-> getConnection (
$ this -> _ resourcePrefix '_' $ connectionName);
} อื่น {
$ this -> _ connections [$ connectionName] = $ this -> _ resources-> getConnection ($ connectionName);
}
}
ส่งคืน $ this -> _ การเชื่อมต่อ [$ connectionName];
}
ขั้นตอนสุดท้ายคือโทรไปยังคอลเลกชันคำสั่งซื้อ แต่ใช้การเชื่อมต่อ test_read
//Get the Order model
$model = Mage::getModel('sales/order');
//set the flag
$model->getResource()->reportConnection = true;
//get the collection
$collection = $model->getCollection();
ในโมดูลของคุณ etc / config.xml เพิ่มรหัสต่อไปนี้:
<global>
<resources>
<modulename_write>
<connection>
<use>modulename_database</use>
</connection>
</modulename_write>
<modulename_read>
<connection>
<use>modulename_database</use>
</connection>
</modulename_read>
<modulename_setup>
<connection>
<use>core_setup</use>
</connection>
</modulename_setup>
<modulename_database>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[db_username]]></username>
<password><![CDATA[db_password]]></password>
<dbname><![CDATA[tablename]]></dbname>
<model>mysql4</model>
<type>pdo_mysql</type>
<active>1</active>
</connection>
</modulename_database>
</resources>
</global>
วิธีรับข้อมูลจากตารางโดยใช้ฐานข้อมูลใหม่:
<?php
$resource = Mage::getSingleton('core/resource');
$conn = $resource->getConnection('modulename_read');
$results = $conn->fetchAll('SELECT * FROM tablename');
echo "<pre>";
print_r($results);
?>