0

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?

1
  • Add your code block in try.....catch block. So If any error occured It show to you. Commented Mar 14, 2018 at 12:09

1 Answer 1

0

I have downloaded your module and checked it for the same.

Below is the two mistakes with your module

1) app/code/Codendot/Getquote/Model/ResourceModel

you have given wrong tablename here(codendot_getquote) but your tablename should be(codendot_getquote_quote) like below , so replace your function with below one :

protected function _construct()
    {
        $this->_init('codendot_getquote_quote', 'quote_id');
    }

2) app/code/Codendot/Getquote/Controller/Index

You have given field name wrong over here , give field name as below :

$model->setProductSku($prod_sku);  // s of Sku should be captial same as for others - as you have given _(underscore) in setup script like product_sku
$model->setCustomerEmail($email); // same as above E capital of Email
$model->setCustomerPhone($phone); // same as above P capital of Phone

Try this , Its worked and data gets saved as well !!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.