如何将输入值保存到数据库magento2

时间:2019-02-16 10:39:25

标签: magento2

我绝对是magento的新手。我在app / design / frontend / NewVendor / NewTheme / Magento_Newsletter / templates / subscribe.phtml

中的电子邮件输入下做了一个名为“ name”的新输入。
<div class="block newsletter">
<div class="title"><strong><?= $block->escapeHtml(__('Newsletter')) ?></strong></div>
<div class="content">
    <form class="form subscribe"
        novalidate
        action="<?= $block->escapeUrl($block->getFormActionUrl()) ?>"
        method="post"
        data-mage-init='{"validation": {"errorClass": "mage-error"}}'
        id="newsletter-validate-detail">
        <div class="field newsletter">
            <label class="label" for="newsletter"><span><?= $block->escapeHtml(__('Sign Up for Our Newsletter:')) ?></span></label>
            <div class="control">
                <input name="email" type="email" id="newsletter"
                       placeholder="<?= $block->escapeHtml(__('Enter your email address')) ?>"
                       data-mage-init='{"mage/trim-input":{}}'
                       data-validate="{required:true, 'validate-email':true}"/>
                <input name="name" placeholder="Name"/>
            </div>

        </div>
        <div class="actions">
            <button class="action subscribe primary" title="<?= $block->escapeHtmlAttr(__('Subscribe')) ?>" type="submit">
                <span><?= $block->escapeHtml(__('Subscribe')) ?></span>
            </button>
        </div>
    </form>
</div>

我在app / code / Mag / Newsletter / Setup / UpgradeSchema.php的newsletter_subscriber表中添加了新列

class UpgradeSchema implements UpgradeSchemaInterface

{

public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    if (version_compare($context->getVersion(), '0.0.2', '<')) {
        $setup->getConnection()->addColumn(
            $setup->getTable('newsletter_subscriber'),
            'name',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 50,
                'nullable' => false,
                'default' => '',
                'comment' => 'Name'
            ]
        );
    }
    $setup->endSetup();
}

}

这是我在app / code / Mag / Newsletter / Controller / Subscriber / NewAction.php中的控制器

<?php
namespace Magebit\Newsletter\Controller\Subscriber;

class NewAction extends 
\Magento\Newsletter\Controller\Subscriber\NewAction
{
    public function execute() {
        $name = $this->getRequest()->getPost();
        var_dump($name);exit;
    }
}

现在,控制器var_dump的输入值。

我要实现的是将输入值保存到“ newsletter_subscriber”表的“名称”列中。

无法正确处理。 接下来我该怎么办?

1 个答案:

答案 0 :(得分:0)

您必须获取Subscriber模型的实例,然后使用本机方法

->setName('NAME')

然后

->save();