0

I am fetching customer Id from magento customer model and a data from custom page and trying to save it in databse.

<?php
namespace SimpleMagento\Milkman\Controller\Account;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use SimpleMagento\Milkman\Model\PrepaidFactory;
use Magento\Customer\Model\SessionFactory;

class AfterPrepaid extends Action
{
    protected $prepaid;
    protected $result;
    protected $customer;
    public function __construct(
        Context $context,
        PrepaidFactory $prepaid,
        ResultFactory $result,
        SessionFactory $customer
    )
    {
        parent::__construct($context);
        $this->result = $result;
        $this->prepaid = $prepaid;
        $this->customer = $customer;
    }

    public function execute()
    {
        $prepaidModel = $this->prepaid->create();
        $entity = $this->getRequest()->getParams();
        $modelcustomer = $this->customer->create();
        $id = $modelcustomer->getCustomer()->getId();
        //$data = $modelcustomer->getCollection()->addFieldToSelect('entity_id')->setOrder('entity_id','DESC');
        $prepaidModel->addData([
            "customer_id" => $id,
            "amount" => $entity['amount']
        ]);
        $model = $prepaidModel->save();
        print_r($model);
    }
}

?>

But I am getting an error.

{"0":"The resource isn't set.","1":"<pre>#1 Magento\\Framework\\Model\\AbstractModel->save() called at [app\/code\/SimpleMagento\/Milkman\/Controller\/Account\/AfterPrepaid.php:39]\n#2 SimpleMagento\\Milkman\\Controller\\Account\\AfterPrepaid->execute() called at [vendor\/magento\/framework\/App\/Action\/Action.php:107]\n#3 Magento\\Framework\\App\\Action\\Action->dispatch(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#4 SimpleMagento\\Milkman\\Controller\\Account\\AfterPrepaid\\Interceptor->___callParent('dispatch', array(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#)) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#5 SimpleMagento\\Milkman\\Controller\\Account\\AfterPrepaid\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#6 SimpleMagento\\Milkman\\Controller\\Account\\AfterPrepaid\\Interceptor->___callPlugins('dispatch', array(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#), array(array('designLoader', 'customerNotifica...', 'catalog_app_acti...', 'tax-app-action-d...', 'weee-app-action-...', 'storeCheck', 'contextPlugin', 'customer-app-act...'))) called at [generated\/code\/SimpleMagento\/Milkman\/Controller\/Account\/AfterPrepaid\/Interceptor.php:26]\n#7 SimpleMagento\\Milkman\\Controller\\Account\\AfterPrepaid\\Interceptor->dispatch(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/App\/FrontController.php:55]\n#8 Magento\\Framework\\App\\FrontController->dispatch(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#9 Magento\\Framework\\App\\FrontController\\Interceptor->___callParent('dispatch', array(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#)) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#10 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/module-store\/App\/FrontController\/Plugin\/RequestPreprocessor.php:94]\n#11 Magento\\Store\\App\\FrontController\\Plugin\\RequestPreprocessor->aroundDispatch(&Magento\\Framework\\App\\FrontController\\Interceptor#00000000350c1720000000000ab7720e#, &Closure#00000000350c1723000000000ab7720e#, &Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:135]\n#12 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/module-page-cache\/Model\/App\/FrontController\/BuiltinPlugin.php:73]\n#13 Magento\\PageCache\\Model\\App\\FrontController\\BuiltinPlugin->aroundDispatch(&Magento\\Framework\\App\\FrontController\\Interceptor#00000000350c1720000000000ab7720e#, &Closure#00000000350c1723000000000ab7720e#, &Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:135]\n#14 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#15 Magento\\Framework\\App\\FrontController\\Interceptor->___callPlugins('dispatch', array(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#), NULL) called at [generated\/code\/Magento\/Framework\/App\/FrontController\/Interceptor.php:26]\n#16 Magento\\Framework\\App\\FrontController\\Interceptor->dispatch(&Magento\\Framework\\App\\Request\\Http#00000000350c1676000000000ab7720e#) called at [vendor\/magento\/framework\/App\/Http.php:136]\n#17 Magento\\Framework\\App\\Http->launch() called at [vendor\/magento\/framework\/App\/Bootstrap.php:257]\n#18 Magento\\Framework\\App\\Bootstrap->run(&Magento\\Framework\\App\\Http\\Interceptor#00000000350c1692000000000ab7720e#) called at [index.php:39]\n<\/pre>","url":"\/magento\/milkman\/Account\/AfterPrepaid\/?amount=1500","script_name":"\/magento\/index.php"}

Install Schema :-

<?php
namespace SimpleMagento\Milkman\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)
    {
        $setup->startSetup();

        $table_two = $setup->getTable('prepaid');

        $tableTwoSetup = $setup->getConnection()->newTable($table_two
        )->addColumn(
            'entity_id',
            Table::TYPE_INTEGER,
            null,
            [
                'identity' => true,
                'nullable' => false,
                'primary' => true
            ],
            'ID'
        )->addColumn(
            'customer_id',
            Table::TYPE_INTEGER,
            null,
            [
                'nullable'=> false
            ],
            'Customer Id'
        )->addColumn(
            'amount',
            Table::TYPE_INTEGER,
            null,
            [
                'nullable'=>false
            ],
            'Amount for postpaid'
        )->setComment("Prepaid_Table");

        $setup->getConnection()->createTable($tableTwoSetup);


        $setup->endSetup();
    }
}
?>
  • please try $prepremode->setData) instread of $prepremode-addtData) – Waqar Ali Dec 19 '19 at 7:59
  • checked..but not working @WaqarAli – Akash Killedar Dec 19 '19 at 10:00
0

Check if your prepaidModel has a constructor with two underlines (__construct) .If exists check if this is valid method e.g passes all parameters correctly for its parent . This methods calls _construct - with one underline- inside it that initiates correctly your resourceModel. For a _construct (one underline) correct method, check below: https://www.mageplaza.com/magento-2-module-development/how-to-create-crud-model-magento-2.html

| 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.