我是magento的新人。我试图在我的控制器的动作中调用模型的方法,但不能这样做。我的文件夹结构是:
我的控制器是:
class Pw_Manageproducts_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$model = Mage::getModel('manageproducts/manageproducts');
//trying to call modelCalls() which is in model class
//$model->modelCalls();
$this->loadLayout();
$this->renderLayout();
}
}
用于模型设置的config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<models>
<manageproducts>
<class>Manageproducts_Model</class>
<resourceModel>Manageproducts_Mysql4</resourceModel>
<manageproducts_mysql4>
<class>Pw_Manageproducts_Model_Mysql4</class>
<entities>
<manageproducts>
<table>blog_posts</table>
</manageproducts>
</entities>
</manageproducts_mysql4>
</manageproducts>
</models>
</global>
</config>
块就像:
class Pw_Manageproducts_Block_Manageproducts extends Mage_Core_Block_Template
{
public function showAll(){
echo "Srimanta";
}
}
模型就像:
class Pw_Manageproducts_Model_Manageproducts extends Mage_Core_Model_Abstract {
public function _construct() {
$this->_init('manageproducts/manageproducts');
}
public function modelCalls(){
return "This is a model function";
}
}
我想在modelCalls()
中调用IndexController
函数。但它没有发生。
我的最终目标是在design/frontend/default/default/template/pw/manageproducts/manageproducts.phtml
的视图页面中显示消息(在模型中的modelCalls()方法和块中的showAll()方法)
请让我知道我在哪里弄错了也请告诉我,不管我的程序是否正确。
提前谢谢。
答案 0 :(得分:1)
您的问题出在资源模型定义
中<resourceModel>Manageproducts_Mysql4</resourceModel>
应该是
<resourceModel>manageproducts_mysql4<resourceModel>
Pw>Manageproducts>Model>Mysql4>Manageproducts.php
的代码
应该是
<?php
class Pw_Manageproducts_Model_Mysql4_Manageproducts extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('manageproducts/manageproducts', 'manageproducts_id');
}
}
更多详情check
答案 1 :(得分:0)
我已经完成了模块中的更改,并根据您的要求进行了检查。它的工作原因请查看下面的代码。
下面是我的config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Test_Helloworld>
<version>0.1.0</version>
<!-- Version of module -->
</Test_Helloworld>
</modules>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>Test_Helloworld</module>
<frontName>helloworld</frontName>
<!-- This is the URL
of the module. i.e www.yourmagento.com/index.php/helloworld will be the url of your module. -->
</args>
</helloworld>
</routers>
<layout>
<updates>
<helloworld>
<file>helloworld.xml</file>
</helloworld>
</updates>
</layout>
</frontend>
<global>
<blocks>
<helloworld>
<class>Test_Helloworld_Block</class>
<!-- Path of the
Block Folder, where all php files are located related to view -->
</helloworld>
</blocks>
<models>
<helloworld>
<class>Test_Helloworld_Model</class>
<resourceModel>helloworld_mysql4</resourceModel>
</helloworld>
<helloworld_mysql4>
<class>Test_Helloworld_Model_Mysql4</class>
<entities>
<helloworld>
<table>friendlist</table>
<!-- Actual table name in sql -->
</helloworld>
</entities>
</helloworld_mysql4>
</models>
<helpers>
<helloworld>
<class>Test_Helloworld_Helper</class>
<!-- Path of Helper Files -->
</helloworld>
</helpers>
<resources>
<!-- These are resource setting giving access to module, read/write permission on database -->
<helloworld_setup>
<setup>
<module>Test_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>
</global>
<adminhtml>
<acl>
<resources>
<admin>
<!--This is acl based on URL. If you see URL it would be /admin/system_config/ -->
<children>
<system>
<children>
<config>
<children>
<test translate="title" module="helloworld">
<!-- This is name of the section created by us -->
<title>Test Section ACL</title>
<!-- Title as shown in User->Roles->Permissions Window -->
<sort_order>99</sort_order>
</test>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
</config>
下面是我的模型文件,我已经声明了我的测试功能。
class Test_Helloworld_Model_Helloworld extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('helloworld/helloworld'); // this is location of the resource file.
}
public function testMassge()
{
return "hi";
}
}
在控制器中调用上面的函数:
<?php echo Mage::getModel('helloworld/helloworld')->testMassge(); ?>
所以请将它与你的模块结构进行比较。它会帮助你。