1

I'm creating basic Module call CustomApplication also I've create a table call vendor_application. When I try to submit my form I get this error Warning.

Illegal offset type in isset or empty in C:\xampp\htdocs\magento_new\vendor\magento\framework\DataObject.php on line 191

Here's my codes :

Submit.php

namespace MKF\CustomApplication\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use MKF\CustomApplication\Model\FormModel;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;

class Submit extends Action
{
    protected $resultPageFactory;
    protected $FormModel;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        FormModel $FormModel
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->FormModel = $FormModel;
        parent::__construct($context);
    }

    public function execute()
    {

        // print_r($this->getRequest()->getPost());

        // exit();

        try {
            $data = (array)$this->getRequest()->getPost();

            //print_r($data); exit();

            if ($data) {
                $model = $this->_FormModel->create();
                $model->setData($data)->save();
                $this->messageManager->addSuccessMessage(__("Data Saved Successfully."));
            }
        } catch (\Exception $e) {
            $this->messageManager->addErrorMessage($e, __("We can\'t submit your request, Please try again."));
        }
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;

    }
}

FormModel.php

namespace MKF\CustomApplication\Model;

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class FormModel extends AbstractModel
{
    protected function _construct()
    {
        $this->_init('MKF\CustomApplication\Model\ResourceModel\VendorApplication');
    }
}

VendorApplication.php

namespace MKF\CustomApplication\Model\ResourceModel;

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

class VendorApplication extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('vendor_application', 'id');
    }
}

Collection.php

namespace MKF\CustomApplication\Model\ResourceModel\VendorApplicationController;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init('MKF\CustomApplication\Model\FormModel', 'MKF\CustomApplication\Model\ResourceModel\VendorApplication');
    }
}

InstallSchema.php

namespace MKF\CustomApplication\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Setup\Exception;
use \Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();
        try {
            $table = $installer->getConnection()->newTable(
                $installer->getTable('vendor_application')
            )
            ->addColumn(
                'id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'nullable' => false, 'primary' => true],
                'Record Id'
            )
            ->addColumn(
                'name',
                Table::TYPE_TEXT,
                255,
                ['nullable' => false],
                'Name'
            )
            ->addColumn(
                'email',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'Email'
            )
            ->addColumn(
                'phone',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'phone'
            )
            ->addColumn(
                'biz_name',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'biz_name'
            )
            ->addColumn(
                'biz_address',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'biz_address'
            )
            ->addColumn(
                'biz_page',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'biz_page'
            )
            ->addColumn(
                'b_prod_cat',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'b_prod_cat'
            )
            ->addColumn(
                'biz_partners',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'biz_partners'
            )
            ->addColumn(
                'biz_short_info',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'biz_short_info'
            )
            ->addColumn(
                'biz_short_info',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'biz_short_info'
            )
            ->addColumn(
                'bank_name',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'bank_name'
            )
            ->addColumn(
                'bank_branch',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'bank_branch'
            )
            ->addColumn(
                'bank_ac',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'bank_ac'
            )
            ->addColumn(
                'bank_routing',
                Table::TYPE_TEXT,
                '255',
                ['nullable' => false],
                'bank_routing'
            )->addColumn(
                'created_at',
                Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
                'Created At'
            )->addColumn(
                'update_time',
                Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE],
                'Updated At'
            )->setComment(
                'Data Table'
            );

            $installer->getConnection()->createTable($table);
            $installer->endSetup();
        } catch (Exception $err) {
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($err->getMessage());
        }
    }
}

Folder Structure

Controller
    |-- Index
    |---|-- Index.php
    |---|-- Submit.php
Model
    |-- ResourceModel
    |---|-- VendorApplicationController
    |-------|-- Collection.php
    |---|-- VendorApplication.php
    |-- FormModel.php
Setup
    |-- InstallSchema.php
    

Thanks in advance.

4
  • have you created the install setup for your table? Commented May 4, 2021 at 16:49
  • yes I did. I'll share the code. Commented May 4, 2021 at 16:52
  • Change $model = $this->_FormModel->create(); to $model = $this->_FormModel; OR change your constructor to to make your FormModel to \MKF\CustomApplication\Model\FormModelFactory and see if that helps. Commented May 5, 2021 at 0:30
  • @AdarshKhatri Thanks, $model = $this->_FormModel; this code works for me. Commented May 5, 2021 at 5:15

1 Answer 1

0

You have instantiated the object directly but using it as a factory class in the code.

Change $model = $this->_FormModel->create(); to $model = $this->_FormModel;

OR

Change your constructor if you want to use factory class:

FormModel $FormModel to \MKF\CustomApplication\Model\FormModelFactory $FormModel

Learn about factory class here: https://devdocs.magento.com/guides/v2.2/extension-dev-guide/factories.html

1
  • Thanks, $model = $this->_FormModel; this code works for me. Commented May 5, 2021 at 6:06

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.