如何通过Event Observer Pattern将自定义字段值保存到magento数据库

时间:2014-07-30 12:48:23

标签: magento events design-patterns

我正在努力通过Newsletter Subscriber模式在Magento Event Observer阻止中添加额外字段。

以下是Observer.php的代码:

<?php

class Scandi_Newsletter_Model_Observer
{
    public function newsletterSubscriberSaveBefore(Varien_Event_Observer $observer){

     $subscriber = $observer->getEvent()->getSubscriber();
     $name = Mage::app()->getRequest()->getParam('subscriber_name');
     $subscriber->setSubscriberName($name);
     $model = Mage::getModel('scandi/newsletter')->setSubscriberName($name)->save();
     //$subscriber->save();
     return $this;
    }
}

?>

我没有得到如何将名称值保存到数据库。

我已经执行了installer脚本&amp;列已添加到数据库现在我需要将名称值保存到数据库。

这是我的config.xml代码: -

   <?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Scandi_Newsletter>
        <version>0.1.0</version>
  </Scandi_Newsletter>
</modules>
<global>
<resources>
        <scandi_newsletter_setup>
        <setup>
            <module>Scandi_Newsletter</module>
            <class>Scandi_Newsletter_Model_Mysql4_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
        </scandi_newsletter_setup>
        <newsletter_write>
    <connection>
    <use>core_write</use>
    </connection>
    </newsletter_write>
    <newsletter_read>
    <connection>
    <use>core_read</use>
    </connection>
    </newsletter_read>

    </resources>
     <events>
   <newsletter_subscriber_save_before>
      <observers>
         <scandi_newsletter_model_observer>
            <type>model</type>
            <class>Scandi_Newsletter_Model_Observer</class>
            <method>newsletterSubscriberSaveBefore</method>
        </scandi_newsletter_model_observer>
      </observers>
   </newsletter_subscriber_save_before>
</events>
</global>   
</config>

非常感谢任何帮助!!

如何在数据库中保存订户名称。

大家好,

我可以打印订阅者名称,我错了我错过了在subscriber.phtml文件中添加订阅者名称。现在我将此代码用于Observer.php: -

<?php

class Scandi_Newsletter_Model_Observer
{
    public function newsletterSubscriberSaveBefore(Varien_Event_Observer $observer) {

    $subscriber = $observer->getEvent()->getSubscriber();
    $name = Mage::app()->getRequest()->getParam('subscriber_name');
 $subscriber->setSubscriberName($name);
 $subscriber->save(); 
    print_r($subscriber->getData());

?>

现在可以任何人协助我如何在数据库中保存这个值&amp;我还需要在管理面板Newsletter Grid中显示这个值。

2 个答案:

答案 0 :(得分:0)

我希望你的安装人员不工作,应该是

<resources>
            <scandi_newsletter_setup>
                <setup>
                    <module>Amit_Custommodule</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </scandi_newsletter_setup>
            <custommoule_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </custommoule_read>
            <scandi_newsletter_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </scandi_newsletter_write>

    </resources>

答案 1 :(得分:0)

Yippe,我可以将值保存在数据库中。这是更新后的代码

包名:-Scandi

模块名称:简报

1.)首先通过添加所需的额外字段来更新subscriber.phtml

2.)Crate Observer.php at 的magento \应用\代码\本地\斯堪\通讯\型号\ Observer.php: -

 <?php

    class Scandi_Newsletter_Model_Observer
    {
        public function newsletterSubscriberSaveBefore(Varien_Event_Observer $observer) {

        $subscriber = $observer->getEvent()->getSubscriber();
        $name = Mage::app()->getRequest()->getParam('subscriber_name');
     $subscriber->setSubscriberName($name);
    ?>

3.)创建安装程序脚本以将自定义字段添加到数据库: - :-app \代码\本地\斯堪\通讯\ SQL \ scandi_newsletter_setup \安装-0.1.0       

    $installer = $this;

    $installer->startSetup();

    $installer->run("
     ALTER TABLE {$installer->getTable('newsletter_subscriber')}
     ADD (`name` TEXT NULL) ");
     $installer->endSetup();

?>

4.。)上面发布的Config.xml