简体   繁体   English

如何在megento中从控制器调用模型类方法?

[英]How to call model class method from controller in megento?

I have created a new model class in checkout/type name Cartpage.php and it has a method name addValue($id) . 我在结帐/类型名称Cartpage.php中创建了一个新的模型类,它具有方法名称addValue($id) Want to call this model class method from saveShippingMethodAction() in OnepageController.php at Checkout/controllers. 要从Checkout / controllers的OnepageController.php中的saveShippingMethodAction()调用此模型类方法。 I have called in several ways but it gives me an error: 我已经以几种方式打电话给我,但它给了我一个错误:

Fatal error: Call to a member function addValue() on a non-object in .../app/code/core/Mage/Checkout/controllers/OnepageController.php 致命错误:在... / app / code / core / Mage / Checkout / controllers / OnepageController.php中的非对象上调用成员函数addValue()

I have tried: 我努力了:

$myModel = Mage::getSingleton('checkout/cartpage');
$return = $myModel->addValue($my_value);

Model: 模型:

class Mage_Checkout_Model_Type_Cartpage extends  Mage_Sales_Model_Quote_Address_Total_Abstract
{
        public function addValue($value)
    {
    return $value;
    }

}

How can I call my custom model from OnepageController? 如何从OnepageController调用自定义模型?

If your module is located under Checkout/Model/Type/Cartpage.php the correct classgroup to call is checkout/type_cartpage 如果您的模块位于Checkout/Model/Type/Cartpage.phpclassgroup调用的正确类classgroupcheckout/type_cartpage
so use Mage::getSingleton('checkout/type_cartpage'); 因此请使用Mage::getSingleton('checkout/type_cartpage'); instead of Mage::getSingleton('checkout/cartpage'); 而不是Mage::getSingleton('checkout/cartpage');

However it looks like you are adding your model class under the core namespace which is a nogo in magento. 但是,似乎您要在core名称空间下添加模型类,这是magento中的nogo。

create you own module und the local namespace and create rewrites for the parts you want/need to change 创建您自己的模块和local名称空间,并为要/需要更改的部分创建rewrites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM