I want to save custom data from Ajax Request in my database but it's not working and not saving any data.
namespace/Getquote/controller/index
<?php
namespace namespace\Getquote\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
class Index extends \Magento\Framework\App\Action\Action
{
protected $resultJsonFactory;
public function __construct
(
\Magento\Framework\App\Action\Context $context,
)
{
return parent::__construct($context);
}
/**
* Get A Quote
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->getRequest()->isAjax()):
$data = $this->getRequest()->getPost();
$prod_sku = 'sku';//$data['prod_sku'];
$email = '[email protected]';//$data['email'];
$phone = '70123456';//$data['phone'];
$__objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $__objectManager->create('namespace\Getquote\Model\Quote');
$model->setProductsku($prod_sku);
$model->setCustomeremail($email);
$model->setCustomerphone($phone);
$model->save();
endif;
}
}
namespace/Model/Quote
<?php
namespace namespace\Getquote\Model;
class Quote extends \Magento\Framework\Model\AbstractModel
{
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
)
{
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
protected function _construct()
{
$this->_init('namespace\Getquote\Model\ResourceModel\Quote');
}
}
namespace/Model/ResourceModel/Quote
<?php
namespace namespace\Getquote\Model\ResourceModel;
class Quote extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context
)
{
parent::__construct($context);
}
protected function _construct()
{
$this->_init('namespace_getquote_quote', 'quote_id');
}
}
namespace/GetQuote/Model/ResourceModel/Quote/Collection
<?php
namespace namespace\Getquote\Model\ResourceModel\Quote;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected $_idFieldName = 'quote_id';
protected $_eventPrefix = 'namespace_getquote_quote_collection';
protected $_eventObject = 'quote_collection';
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('namespace\Getquote\Model\Quote', 'namespace\Getquote\Model\ResourceModel\Quote');
}
}
InstallSchema.php
<?php
namespace namespace\Getquote\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
if (!$installer->tableExists('namespace_getquote_quote')) {
$table = $installer->getConnection()->newTable(
$installer->getTable('namespace_getquote_quote')
)
->addColumn(
'quote_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'nullable' => false,
'primary' => true,
'unsigned' => true,
],
'Quote ID'
)
->addColumn(
'product_sku',
Table::TYPE_TEXT,
255,
['nullable' => false,'default' => ''],
'Product SKU'
)
->addColumn(
'customer_email',
Table::TYPE_TEXT,
255,
['nullable' => false,'default' => ''],
'Email'
)
->addColumn(
'customer_phone',
Table::TYPE_TEXT,
255,
['nullable' => false,'default' => ''],
'Phone Number'
)
->addColumn(
'created_at',
Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
'Created At'
)->addColumn(
'updated_at',
Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE],
'Updated At')
->setComment('Quote Table')
->setOption('charset', 'utf8');
$installer->getConnection()->createTable($table);
}
$installer->endSetup();
}
}
Any suggestions please?