เกิดข้อผิดพลาดในการสร้างโมดูลในวีโอไอพี

ฉันกำลังพยายามสร้างโมดูลที่กำหนดเองในวีโอไอพีในครั้งแรก แต่ฉันมีข้อผิดพลาดบางอย่าง ฉันได้กล่าวถึงรหัสโมดูลทั้งหมดของฉันที่นี่ และมันทำให้ฉันมีข้อผิดพลาด "ข้อผิดพลาดร้ายแรง: การเรียกไปยังฟังก์ชันสมาชิก setOrder() บนวัตถุที่ไม่ใช่ใน /var/www/magentotest/app/code/local/Mycustommodule/Helloworld/Block /Monblock.php"

pls help me.


(A) directory structure
===============================================================
1. in app/code/local
Mycustommodule/Helloworld/etc/config.xml
Mycustommodule/Helloworld/controllers/IndexController.php
Mycustommodule/Helloworld/Model/Helloworld.php
Mycustommodule/Helloworld/Model/Mysql4/Helloworld.php
Mycustommodule/Helloworld/Model/Mysql4/Helloworld/Collection.php
Mycustommodule/Helloworld/Block/Monblock.php

2. in design 

app/design/frontend/default/default/template/helloworld/view.phtml
app/design/frontend/default/default/layout/helloworld.xml

3. my table is 
id_mycustommodule_helloworld    nom     prenom  telephone
1                               abc     s1      435355353
2                               fgh     s2      55555

===================================================================

(B) now my code
===============================================================

1. config.xml
**************

<?xml version="1.0" ?>
<config>
    <modules>
        <Mycustommodule_Helloworld>
            <version>0.0.1</version>
        </Mycustommodule_Helloworld>
    </modules>

    <frontend>
        <routers>
            <helloworld>
                <use>standard</use>
                <args>
                    <module>Mycustommodule_Helloworld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>

        <!-- This node contains module layout configuration -->
        <layout>
            <updates>
                <!-- module alias -->
                <helloworld>
                    <!-- File for loading -->
                    <file>helloworld.xml</file>
                </helloworld>
            </updates>
        </layout>
    </frontend>
    <global>

        <models>
            <helloworld>
                 <class>Mycustommodule_Helloworld_Model</class>
                 <resourceModel>helloworld_mysql4</resourceModel>
             </helloworld>
             <helloworld_mysql4>
                 <class>Mycustommodule_Helloworld_Model_Mysql4</class>
                 <entities>
                     <helloworld>
                       <table>mycustommodule_helloworld</table>
                     </helloworld>
                  </entities>
            </helloworld_mysql4> 
        </models>

        <resources>

            <helloworld_setup>
                <setup>
                  <module>Mycustommodule_Helloworld</module>
                </setup>
                <connection>
                  <use>core_setup</use>
                </connection>
            </helloworld_setup>

            <helloworld_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </helloworld_write>

            <helloworld_read>
              <connection>
                 <use>core_read</use>
              </connection>
            </helloworld_read>
        </resources>
        <blocks>
             <helloworld>
                  <class>Mycustommodule_Helloworld_Block</class>
             </helloworld>
        </blocks>
  </global>
</config>


2. IndexController.php
***********************

<?php
class Mycustommodule_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    public function methodAction()
    {
          echo "block called";
    }
}
?>

3. Mycustommodule/Helloworld/Model/Helloworld.php
**************************************************
<?php
class Mycustommodule_Helloworld_Model_Helloworld extends Mage_Core_Model_Abstract
{
     public function _construct()
     {
         parent::_construct();
         $this->_init('helloworld/helloworld');
     }
}
?>

4. Mycustommodule/Helloworld/Model/Mysql4/Helloworld.php
**************************************************
<?php
class Mycustommodule_Helloworld_Model_Mysql4_Helloworld extends Mage_Core_Model_Mysql4_Abstract
{
     public function _construct()
     {
         $this->_init('helloworld/helloworld', 'id_MyCustomModule_helloworld');
     }
}
?>

5. Mycustommodule/Helloworld/Model/Mysql4/Helloworld/Collection.php
********************************************************************
<?php
class Mycustommodule_Helloworld_Model_Mysql4_Helloworld_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
 {
     public function _construct()
     {
         parent::_construct();
         $this->_init('helloworld/helloworld');
     }
}
?>

6. Mycustommodule/Helloworld/Block/Monblock.php
********************************************************************
<?php
class Mycustommodule_Helloworld_Block_Monblock extends Mage_Core_Block_Template
{
     public function methodblock()
     {

        $retour='';
        $collection = Mage::getModel('helloworld/helloworld')->getCollection()
                                 ->setOrder('id_mycustommodule_helloworld','asc');

        foreach($collection as $data)
        {
             $retour .= $data->getData('nom').' '.$data->getData('prenom')
                     .' '.$data->getData('telephone').'<br />';
        }
        Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
        return $retour;
    }
}
?>


7. app/design/frontend/default/default/template/helloworld/view.phtml
********************************************************************

<p>Hello World!</p>

<?php 
 echo $this->methodblock();
?>


8. app/design/frontend/default/default/layout/helloworld.xml
****************************************************************
<?xml version="1.0" ?>
<layout version="0.1.0">

    <helloworld_index_index>
        <reference name="content">
            <block type="helloworld/monblock" name="helloworld" template="helloworld/view.phtml"></block>
        </reference>
    </helloworld_index_index>
</layout>

person divyang asodiya    schedule 12.07.2013    source แหล่งที่มา


คำตอบ (1)


ปัญหาเกิดขึ้นกับบรรทัดนี้:

    $collection = Mage::getModel('helloworld/helloworld')->getCollection()
                             ->setOrder('id_mycustommodule_helloworld','asc');

getCollection() จะไม่ส่งคืนคอลเล็กชัน ซึ่งอาจเป็นเพราะไม่มีคลาสทรัพยากรที่เกี่ยวข้องกับโมดูลใหม่ของคุณ

บทความนี้ มีคำอธิบายที่ดีและคำอธิบาย เกี่ยวกับคลาสทรัพยากร

ฉันยังไม่ได้ทดสอบสิ่งนี้ แต่ฉันคิดว่าวิธีแก้ปัญหาจะเป็นเช่นนี้ ...

คุณต้องการสร้างคลาสใน Mycustommodule/Helloworld/Resource/Helloworld/Collection.php ที่มีลักษณะดังนี้

class Mycustomermodule_HelloWorld_Resource_Helloworld_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    // not the same as PHP's __construct()
    function _construct() {
        $this->_init("helloworld/helloworld", "id_MyCustomModule_helloworld");
    }
}

รวมถึงเพิ่มโหนดนี้พร้อมค่าให้กับไฟล์ config.xml ของคุณ config/global/resources/helloworld/class => Mycustommodule_Helloworld_Resource

person Andy Jones    schedule 12.07.2013
comment
ใช่ ฉันได้เห็นลิงก์ของคุณที่ pixafy.com/ blog/2013/04/creating-a-magento-custom-model และมันได้ผลสำหรับฉัน - person divyang asodiya; 13.07.2013