如何将自定义表单中的 post 数据添加到 Magento 2 中的自定义 table?

How to add post data from custom form to custom table in Magento 2?

我已经浏览了很多 link,但没有找到任何相关的答案。 我正在使用 Model/ResourceModel 进行此操作,在打印时我从自定义表单中获取 post 数据,但未将其插入到自定义 table.

请分享代码或参考 link。

提前致谢

第 1 步:创建一个 xml 文件:routename_controllername_actionname.xml 调用 phtml 文件

    <?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="CustomB2BRFQ\Module\Block\RequestForQuoteForm" name="custom_module_form"
                   template="CustomB2BRFQ_Module::rfq.phtml" />
        </referenceContainer>
    </body>
</page>

第 2 步:创建您的 phtml 文件:就像我的情况一样

<div class="row">
    <div class="col-md-8">
        <form name="addData" method="post" id="addData" class="form" action="<?php echo $this->getFormAction(); ?>" data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
         data-mage-init='{"validation":{}}'>
            <fieldset class="fieldset">
            <legend class="legend"><span>Request For Quote</span></legend>
                <fieldset class="fieldset row">
                    <div class="fields col-md-6">
                        <div class="field name required">
                            <label class="label" for="uname"><span>Name</span></label>
                            <div class="control">
                            <input name="uname" id="uname" title="Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="email"><span>Email</span></label>
                            <div class="control">
                                <input name="email" id="email" title="Email" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>                       
                        <div class="field required">
                            <label class="label" for="category"><span>Category</span></label>
                            <div class="control">
                                <input name="category" id="category" title="Category" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="company"><span>Company</span></label>
                            <div class="control">
                                <input type="text" name="company" id="company"  data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="gstnumber"><span>GST Number</span></label>
                            <div class="control">
                                <input type="text" name="gstnumber" id="gstnumber"  data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="phonenumber"><span>Phone Number</span></label>
                            <div class="control">
                                <input type="text" name="phonenumber" id="phonenumber"  data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="itemsrequired"><span>Items Required</span></label>
                            <div class="control">
                                <input type="text" name="itemsrequired" id="itemsrequired"  data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="location"><span>Location</span></label>
                            <div class="control">
                                <input type="text" name="location" id="location"  data-validate="{required:true}">
                            </div>
                        </div>
                        
                    </div>
            </fieldset>
            </fieldset>
        <div class="actions-toolbar">
            <div class="primary">
                <button type="submit" class="action submit primary" title="Save"><span>Submit Request</span></button>
            </div>
        </div>
        </form>
    </div>
</div>

第 3 步:将逻辑添加到控制器文件以将数据保存到数据库:

<?php
namespace CustomB2BRFQ\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;
    
    /**
     * @var \CustomB2BRFQ\Module\Model\quoteFactory
     */
    protected $requestForQuoteFactory;


    /**
     * @var \CustomB2BRFQ\Module\Model\quote
     */
    protected $requestForQuote;
    
    
    /**      * @param \Magento\Framework\App\Action\Context $context      */
    public function __construct(\Magento\Framework\App\Action\Context $context, 
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \CustomB2BRFQ\Module\Model\RequestForQuoteFactory $requestForQuoteFactory,
    \CustomB2BRFQ\Module\Model\RequestForQuote $requestForQuote)
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->requestForQuoteFactory = $requestForQuoteFactory;
        $this->requestForQuote = $requestForQuote;
        parent::__construct($context);
    }
    /**
     * Blog Index, shows a list of recent blog posts.
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
     public function execute()
    {
         //return $resultPage = $this->resultPageFactory->create();die;
        $resultRedirect = $this->resultRedirectFactory->create();
        try{

            $request = $this->getRequest()->getParams();
            if(isset($request) && isset($request['uname']) && isset($request['email']) && isset($request['company'])
            && isset($request['category']) && isset($request['gstnumber']) && isset($request['itemsrequired']) && isset($request['location'])
            && isset($request['phonenumber'])){
            $uname = $request['uname'];
            $email = $request['email'];
            $company = $request['company'];
            $category = $request['category'];
            $gstnumber = $request['gstnumber'];
            $itemsrequired = $request['itemsrequired'];
            $location = $request['location'];
            $phonenumber = $request['phonenumber'];
            
            $requestquote = $this->requestForQuoteFactory->create();
            $requestquote->setName($uname);
            $requestquote->setEmail($email);
            $requestquote->setCompany($company);
            $requestquote->setCategory($category);
            $requestquote->setGstNumber($gstnumber);
            $requestquote->setItemsRequired($itemsrequired);
            $requestquote->setLocation($location);
            $requestquote->setPhoneNumber($phonenumber);

            $requestquote->save();
            $message = __('Quote is successfully submitted.');
            $this->messageManager->addSuccessMessage($message);
            $this->resultPageFactory->create();
            return $resultRedirect->setPath('quote/index/index');
        
        }else{
        $message = __('Filled all the details.');
        $this->messageManager->addSuccessMessage($message);
        return $this->resultPageFactory->create();
    }
          
        }catch (\Exception $e){
            $this->messageManager->addException($e, __('We can\'t submit your request, Please try again.'));
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
            return $resultRedirect->setPath('quote/index/index');
        }
    }
}

谢谢! 我试过了,效果很好