Mage::getModel คืนค่าเท็จ

ฉันกำลังพยายามทำความเข้าใจกับการโทรและการใช้โมเดลในวีโอไอพี

งานปัจจุบันของฉันคือเพียงแสดงสตริงที่อยู่ในโมเดลโดยการเรียกมันจากคอนโทรลเลอร์

เมื่อพยายามโทร SimpleOutput.php ฉันได้รับข้อความแสดงข้อผิดพลาดแจ้งว่ามีการเรียกใช้วัตถุที่ไม่ใช่ ฉันได้ var_dumped มันอย่างที่คุณเห็นแล้วมันก็คืนค่าเท็จ

ฉันได้ดูโค้ดของฉันแล้ว และด้วยความเข้าใจที่จำกัดเกี่ยวกับสิ่งที่ฉันต้องทำใน Magento ฉันจึงมีทุกอย่างถูกต้อง แน่นอนว่าฉันขาดอะไรบางอย่างไป ใครช่วยดูหน่อยได้ไหม และถ้ามันพิมพ์ผิดก็บอกได้ว่าต้องดูที่ไหน และถ้ามันเป็นมากกว่าการสะกดผิดธรรมดาๆ ให้อธิบายสิ่งที่ฉันพลาดไป และสิ่งที่ฉันควรทำ และเพราะเหตุใด

รหัสของฉันอยู่ด้านล่าง

Ts/Firstmodule/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Ts_Firstmodule>
        <version>0.1.0</version>
    </Ts_Firstmodule>
</modules>

<models>
    <firstmodule>
        <class>Ts_Firstmodule_Model</class>
    </firstmodule>
</models>

<frontend>
    <routers>
        <firstmodule>
            <use>standard</use>
            <args>
                <module>Ts_Firstmodule</module>
                <frontName>firstmodule</frontName>
            </args>
        </firstmodule>
    </routers>
</frontend>
</config>

Ts/Firstmodule/คอนโทรลเลอร์/indexController.php

class Ts_Firstmodule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
    $simple = Mage::getModel('ts_firstmodule/simpleoutput');
    var_dump($simple);
}
}

Ts/Firstmodule/model/simpleoutput.php

class Ts_Firstmodule_Model_SimpleOutput extends Mage_Core_Model_Abstract
{
public function basicText()
{
    echo 'this is some text from the simple output model inside the basic text function';
}
}

person tony09uk    schedule 11.07.2013    source แหล่งที่มา


คำตอบ (3)


คุณต้องล้อม <models> ใน <global> ดังนี้:

<global>
    <models>
        <firstmodule>
            <class>Ts_Firstmodule_Model</class>
        </firstmodule>
    </models>
</global>

อย่าลังเลที่จะดูที่มาของโมดูลหลักที่เรียบง่ายกว่า (เช่น GoogleAnalytics) เพื่อดูว่าพวกมันทำงานอย่างไรและเข้าใจตรรกะเบื้องหลัง

person blmage    schedule 11.07.2013
comment
ขอบคุณที่ได้ผล สำหรับการดูโค้ดโมดูลง่ายๆ ฉันจะเริ่มก่อน แนวคิดยอดนิยม - person tony09uk; 11.07.2013

เช่นเคย :

Mage::getModel('ts_firstmodule/simpleoutput');

เมื่อคุณทำ getModel / getBlock / helper / ฯลฯ

ส่วนแรกของสตริงพารามิเตอร์คือโหนด XML ของเลเยอร์ที่กำหนดใน config.xml ส่วนที่สองคือเส้นทางแบบเต็มไปยังไฟล์ของคุณจากคอนเทนเนอร์โฟลเดอร์เลเยอร์

ดังนั้นในกรณีของคุณ : Mage::getModel('firstmodule/simpleoutput'); ควรโหลด Ts/Firstmodule/Model/Simpleoutput.php

หมายเหตุ : ระวังกรณีทรัพยากรของคุณ (ดูวีโอไอพีมาตรฐานสำหรับแนวทางปฏิบัติที่ดี) !

person Jscti    schedule 11.07.2013

คุณควรแก้ไขไฟล์ config.xml และเพิ่มแท็ก <global> รอบๆ แท็ก <models> ของคุณ:

<global>
    <models>
        <firstmodule>
            <class>Ts_Firstmodule_Model</class>
        </firstmodule>
    </models>
<global>

หลังจากนี้ เพื่อที่จะยกตัวอย่างโมเดลให้ใช้ดังนี้:

Mage::getModel('firstmodule/simpleoutput')

ส่วนแรกของเมธอด getModel (จนถึง /) ควรเป็นชื่อแท็กที่คุณตั้งไว้ใน config.xml ใต้แท็ก <models> ในกรณีของคุณ firstmodule

person Marius    schedule 11.07.2013