如何使用两个表进行单个扩展并在 Magento 中显示结果?

How to use two tables for single extension and show results in Magento?

我正在 Magento 中进行扩展,并尝试从两个表中获取数据以显示在一个块中。我想知道如何满足和定义模型和扩展 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');
     }
}

Test.php 在 Mysql4:

<?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');
     }
}

在 entities 标签中,您可以定义另一个 table with 标签。 例如

<entities>
 <test>
   <table>pfay_test</table>
 </test>
 <test2>
   <table>pfay_test2</table>
 </test2>
</entities>

我按照下面的方法做了,效果很好。

我写的代码如下:

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');
     }
}

Test.php 在 Mysql4:

<?php
class Pfay_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
{
     public function _construct()
     {
         $this->_init('test/test', 'id_pfay_test');
     }
}

Test2.php 在 Mysql4:

<?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');
     }
}

Collection.php Test2 文件夹中的代码:

<?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;
    }