0

My controller file

 <?php
namespace Afg\Dealerreferral\Controller\Index;
use Afg\Dealerreferral\Model\DataFactory;
use Magento\Framework\Controller\ResultFactory;

class  formdata extends \Magento\Framework\App\Action\Action{
   protected $Data;
   protected $resultRedirect;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
       // \Magento\Framework\Controller\ResultFactory $result
        \Afg\Dealerreferral\Model\DataFactory  $data

    ) {
        $this->Data = $data;
        parent::__construct($context);
      //  $this->resultRedirect = $result;
    }

    public function execute(){

        $data = $this->getRequest()->getPostValue();
        //$model = $this->DataFactory->create();
      //  $model->setData($data)->save();



        $this->messageManager->addSuccess(__('Form successfully submitted'));

        $this->_redirect('form');
    }


}

My model file

    <?php
namespace Afg\Dealerreferral\Model;
use Magento\Framework\Model\AbstractModel;
class Data extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Afg\Dealerreferral\Model\ResourceModel\Data');
    }
}

My resource model file

    <?php
namespace Afg\Dealerreferral\Model\ResourceModel;
class Data  extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb{
    public function _construct(){
        $this->_init("Dealerreferral","ref_id");
    }
}

And collection file

    <?php
namespace Afg\Dealerreferral\Model\ResourceModel\Data;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Define model & resource model
*/
protected function _construct()
{
$this->_init(
'Afg\Dealerreferral\Model',
'Afg\Dealerreferral\Model\ResourceModel\Data'
);
}
}

This error I am facing

    1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Afg\Dealerreferral\Controller\Index\formdata\Interceptor

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Afg\Dealerreferral\Controller\Index\formdata\Interceptor
<pre>#1 Magento\Framework\ObjectManager\Factory\Compiled->create() called at [vendor/magento/framework/ObjectManager/ObjectManager.php:56]
#2 Magento\Framework\ObjectManager\ObjectManager->create() called at [vendor/magento/framework/App/ActionFactory.php:44]
#3 Magento\Framework\App\ActionFactory->create() called at [vendor/magento/framework/App/Router/Base.php:306]
#4 Magento\Framework\App\Router\Base->matchAction() called at [vendor/magento/framework/App/Router/Base.php:167]
#5 Magento\Framework\App\Router\Base->match() called at [vendor/magento/framework/App/FrontController.php:95]
#6 Magento\Framework\App\FrontController->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#7 Magento\Framework\App\FrontController\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#8 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php:94]
#9 Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#10 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [app/code/Magestorm/ReCaptcha/Plugin/PreDispatch.php:137]
#11 Magestorm\ReCaptcha\Plugin\PreDispatch->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#12 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php:69]
#13 Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#14 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#15 Magento\Framework\App\FrontController\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#16 Magento\Framework\App\FrontController\Interceptor->dispatch() called at [vendor/magento/framework/App/Http.php:137]
#17 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#18 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#19 Magento\Framework\App\Bootstrap->run() called at [pub/index.php:40]
</pre>
  • it may be an issue of model i guess. – Mustafa Ali Apr 15 at 10:01
1

Try this way

Model file

app\code\Afg\Dealerreferral\Model

CustomerData.php

<?php


namespace Afg\Dealerreferral\Model;

use Magento\Framework\Model\AbstractModel;
use Afg\Dealerreferral\Model\ResourceModel\CustomerData as CustomerDataResourceModel;

class CustomerData extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(CustomerDataResourceModel::class);
    }
}

Resource model file

app\code\Afg\Dealerreferral\Model\ResourceModel

CustomerData.php

<?php

namespace Afg\Dealerreferral\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class CustomerData extends AbstractDb
{
    protected function _construct()
    {
        //palese check you table name and primary key column name
        $this->_init('Dealerreferral', 'ref_id'); 
    }
}

Collection file

app\code\Afg\Dealerreferral\Model\ResourceModel\CustomerData

Collection.php

<?php

namespace Afg\Dealerreferral\Model\ResourceModel\CustomerData;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Afg\Dealerreferral\Model\CustomerData as CustomerDataModel;
use Afg\Dealerreferral\Model\ResourceModel\CustomerData as CustomerDataResourceModel;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            CustomerDataModel::class,
            CustomerDataResourceModel::class
        );
    }
}

Controller file

<?php

namespace Afg\Dealerreferral\Controller\Index;

class  formdata extends \Magento\Framework\App\Action\Action{

   protected $cutomerData;
   protected $resultRedirect;

   public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Afg\Dealerreferral\Model\CustomerDataFactory  $cutomerData

    ) {
        $this->cutomerData = $cutomerData;
        parent::__construct($context);
    }

    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        //print_r($data);
        $model = $this->cutomerData->create();
        $model->setData($data);
        $model->save();
        // your save data
        echo "<pre>";
        print_r($model->getData());
        exit();        
    }
}

Please run magent command

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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