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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
scroll top