从另一个控制器magento

时间:2016-02-10 12:23:03

标签: magento

我有一个控制器,比如{magento_root_dir} / app / code / core / Mage / Contacts / controllers中的控制器ClassA和一个模型对象,比如ModelB in

{magento_root_dir} /应用程序/代码/本地/ Homeliv /信息/控制器。

现在我需要从ClassA访问ModelB,以便我可以将modelB保存在数据库中。

我做到了:

            $lead = Mage::getModel('local/homeliv/leads/homelivleads');
            $lead->setName('Code Post!');
            $lead->setEmail('This post was created from code!');
            $lead->setMobile(9999);
            $lead->save();

它的投掷错误。我在这里做错了什么?

config.xml中

<config>
    <modules>
        <Homeliv_Leads>
            <version>0.1.0</version>
        </Homeliv_Leads>
    </modules>
    <global>
    <!-- ... -->
    <models>
        <leads>
            <class>Homeliv_Leads_Model</class>
            <!--
            need to create our own resource, can't just
            use core_resource
            -->
            <resourceModel>leads_resource</resourceModel>
        </leads>
        <leads_resource>
            <class>Homeliv_Leads_Model_Resource</class>
            <entities>
                <homelivleads>
                    <table>homeliv_leads</table>
                </homelivleads>
            </entities>

        </leads_resource>
    </models>
    <!-- ... -->
</global>
</config>

/var/www/html/app/code/local/Homeliv/Leads/Model/HomelivLeads.php:

class Homeliv_Leads_Model_HomelivLeads extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('leads/homelivleads');
    }
}

/var/www/html/app/code/local/Homeliv/Leads/Model/Resource/HomelivLeads.php:

class Homeliv_Leads_Model_Resource_HomelivLeads extends Mage_Core_Model_Resource_Db_Abstract{
    protected function _construct()
    {
        $this->_init('leads/homelivleads', 'id');
    }
}

/var/www/html/app/code/local/Homeliv/Leads/Model/Resource/HomelivLeads/Collection.php:

class Homeliv_Leads_Model_Resource_HomelivLeads_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    protected function _construct()
    {
            $this->_init('leads/homelivleads');
    }
}

/无功/网络/ HTML /应用程序的/ etc /模块

<config>
    <modules>
        <Homeliv_Leads>
            <active>true</active>
            <codePool>local</codePool>
        </Homeliv_Leads>
    </modules>
</config>

1 个答案:

答案 0 :(得分:2)

如果你在这里创建了一个单独的模型&#34; app / code / local / Homeliv / Leads / Model /&#34;,那么你可以使用

$lead = Mage::getModel('leads/homelivleads');