我正在Magento中进行扩展,并尝试从两个表中获取数据以显示在块中。我想知道如何在Model和扩展config.xml文件中提供和定义两个表?
我写的代码如下:
config.xml文件代码:
<?xml version="1.0"?>
<config>
<modules>
<Pfay_Test>
<version>0.1.0</version>
</Pfay_Test>
</modules>
<frontend>
<routers>
<testfrontend>
<use>standard</use>
<args>
<module>Pfay_Test</module>
<frontName>test</frontName>
</args>
</testfrontend>
</routers>
<layout>
<updates>
<test>
<file>test.xml</file>
</test>
</updates>
</layout>
</frontend>
<global>
<blocks>
<test>
<class>Pfay_Test_Block</class>
</test>
</blocks>
<models>
<test>
<class>Pfay_Test_Model</class>
<resourceModel>test_mysql4</resourceModel>
</test>
<test_mysql4>
<class>Pfay_Test_Model_Mysql4</class>
<entities>
<test>
<table>pfay_test</table>
</test>
</entities>
</test_mysql4>
</model>
<resources>
<test_write>
<connection>
<use>core_write</use>
</connection>
</test_write>
</resources>
</global>
</config>
我的模型层次结构如下:
-->Model
-->Test.php
-->Mysql4
-->Test.php
-->Test
-->Collection.php
文件中受尊重的代码如下:
模型中的Test.php:
<?php
class Pfay_Test_Model_Test extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('test/test');
}
}
Mysql4中的Test.php:
<?php
class Pfay_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('test/test', 'id_pfay_test');
}
}
测试文件夹中的Collection.php代码:
<?php
class Pfay_Test_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('test/test');
}
}
答案 0 :(得分:0)
在实体标记内,您可以使用标记定义另一个表。 例如
<entities>
<test>
<table>pfay_test</table>
</test>
<test2>
<table>pfay_test2</table>
</test2>
</entities>
答案 1 :(得分:0)
我已经按照以下方式完成了它并且它有效。
我写的代码如下:
config.xml文件代码:
<?xml version="1.0"?>
<config>
<modules>
<Pfay_Test>
<version>0.1.0</version>
</Pfay_Test>
</modules>
<frontend>
<routers>
<testfrontend>
<use>standard</use>
<args>
<module>Pfay_Test</module>
<frontName>test</frontName>
</args>
</testfrontend>
</routers>
<layout>
<updates>
<test>
<file>test.xml</file>
</test>
</updates>
</layout>
</frontend>
<global>
<blocks>
<test>
<class>Pfay_Test_Block</class>
</test>
</blocks>
<models>
<test>
<class>Pfay_Test_Model</class>
<resourceModel>test_mysql4</resourceModel>
</test>
<test_mysql4>
<class>Pfay_Test_Model_Mysql4</class>
<entities>
<test>
<table>pfay_test</table>
</test>
<test2>
<table>poll</table>
</test2>
</entities>
</test_mysql4>
</models>
<resources>
<test_write>
<connection>
<use>core_write</use>
</connection>
</test_write>
<test_read>
<connection>
<use>core_read</use>
</connection>
</test_read>
</resources>
</global>
</config>
我的模型层次结构如下:
-->Model
-->Test.php
-->Test2.php
-->Mysql4
-->Test.php
-->Test2.php
-->Test
-->Collection.php
-->Test2
-->Collection.php
文件中受尊重的代码如下:
模型中的Test.php:
<?php
class Pfay_Test_Model_Test extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('test/test');
}
}
模型中的Test2.php:
<?php
class Pfay_Test_Model_Test2 extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('test/test2');
}
}
Mysql4中的Test.php:
<?php
class Pfay_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('test/test', 'id_pfay_test');
}
}
Mysql4中的Test2.php:
<?php
class Pfay_Test_Model_Mysql4_Test2 extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('test/test2', 'poll_id');
}
}
测试文件夹中的Collection.php代码:
<?php
class Pfay_Test_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('test/test');
}
}
Test2文件夹中的Collection.php代码:
<?php
class Pfay_Test_Model_Mysql4_Test2_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('test/test2');
}
}
块中的用法是:
public function methodblock()
{
//on initialize la variable
$retour='';
/* we are doing the query to select all elements of the pfay_test table (thanks to our model test/test and we sort them by id_pfay_test */
$collection = Mage::getModel('test/test')->getCollection()
->setOrder('id_pfay_test','asc');
//then, we check the result of the query and with the function getData()
foreach($collection as $data)
{
$retour .= $data->getData('nom').' '.$data->getData('prenom').' '.$data->getData('telephone').'<br />';
}
//i return a success message to the user thanks to the Session.
$collection2 = Mage::getModel('test/test2')->getCollection()
->setOrder('poll_id','asc');
foreach($collection2 as $data)
{
$retour .= $data->getData('poll_title').' '.$data->getData('votes_count').' '.$data->getData('date_posted').'<br />';
}
Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
return $retour;
}