1

Interceptor generation error: The requested class did not generate properly, because the 'generated' directory permission is read-only. If --- after running the 'bin/magento setup:di:compile' CLI command when the 'generated' directory permission is set to write.

I also want to save the form data custom db table named "prescription_form"

My Controller code is:

<?php

namespace Softadroit\Prescription\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\ObjectManager;

class Save extends \Magento\Framework\App\Action\Action
{       
    protected $_pageFactory;

    protected $_postFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Softadroit\Prescription\Controller\Index $postFactory
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->_postFactory = $postFactory;
        return parent::__construct($context);
    }
    
    public function execute()
    {
        $post = $this->getRequest()->getPost();
        
        
        if (!empty($post)) {
            $name   = $post['name'];
            $age    = $post['age'];
            $weight = $post['weight'];
            $height = $post['height'];
            $gender = $post['gender'];
            $product_id = $post['product_id'];
            $customer_id = $post['customer_id'];
        
            $model = $this->_objectManager->create('Softadroit\Prescription\Model\Prescription');           
            /* $model->setName($name);
            $model->setAge($age);
            $model->setWeight($weight);
            $model->setHeight($height);
            $model->setGender($gender);
            $model->setGender($product_id);
            $model->setGender($customer_id); */
            $model->setData([
                    "name" => $name,
                    "age" => $age,
                    "weight" => $weight,
                    "height" => $height,
                    "gender" => $gender,
                    "product_id" => $product_id,
                    "customer_id" => $customer_id
                    ]);
        
        /* echo "<pre>";
        print_r($model);
        die(); */
            
             $msg ="";
             $data =  $model->save();
            if($data != ''){ 
                //$model->save(); 
                $this->messageManager->addSuccessMessage('Thanks your form is submitted successfully!');
            }else{ 
                $this->messageManager->addSuccessMessage('Error in submitting data. Please contact on this email!'); 
            } 
            echo $msg; 
            
            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/prescription/index');
            return $resultRedirect;
        }
       
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

Resource model code:

<?php
namespace Softadroit\Prescription\Model\ResourceModel;


class Prescription 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('prescription_form', 'id');
    }
    
}

Model code:

<?php
namespace Softadroit\Prescription\Model;
class Prescription extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
    const CACHE_TAG = 'prescription_form';

    protected $_cacheTag = 'prescription_form';

    protected $_eventPrefix = 'prescription_form';

    protected function _construct()
    {
        $this->_init('Softadroit\Prescription\Model\ResourceModel\Prescription');
    }

    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }

    public function getDefaultValues()
    {
        $values = [];

        return $values;
    }
}

HTML template code I used to make form:

<?php
// Get all visible items in cart
$quote = $block->getQuoteData();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
?>

<div class="row">
    <div class="col-md-8">
        <form name="addData" method="post" id="addData" class="form" action="<?php echo $this->getFormAction(); ?>" data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>" data-mage-init='{"validation":{}}'>
        <?php foreach($quote->getAllVisibleItems() as $_item) { 
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());
            $customerId = $block->getLoggedinCustomerId();
            //echo $product->getPrescription(); //die();
            $is_priscription = $product->getData('prescription');
            if($is_priscription != "" && $is_priscription == '5431'){  // change prescription option id here ?>
            <fieldset class="fieldset">
                <legend class="legend"><span>Add Prescription: <?php echo $_item->getName(); ?></span></legend>
                <fieldset class="fieldset row">
                    <div class="fields col-md-6">
                        <div class="field name required">
                            <label class="label" for="name"><span>Name</span></label>
                            <div class="control">
                            <input name="name[]" id="name" title="Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="age"><span>Age</span></label>
                            <div class="control">
                                <input name="age[]" id="Age" title="Age" class="input-text" type="int" data-validate="{required:true}"> Years</input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="height"><span>Height</span></label>
                            <div class="control">
                                <input name="height[]" id="Height" title="Height" class="input-text" type="text" data-validate="{required:true}"> FT</input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="weight"><span>Weight</span></label>
                            <div class="control">
                                <input name="weight[]" id="Weight" title="Weight" class="input-text" type="int" data-validate="{required:true}"> KG</input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="gender"><span>Gender</span></label>
                            <select name="subject_gender[]">
                            <option>---Select Gender--</option>
                            <option value="male">Male</option>
                            <option value="female">Female</option>
                            </select>
                        </div>                      
                        <!--<div class="field date required">
                            <label class="label" for="publish_date"><span>Publish Date</span></label>
                            <div class="control">
                                <input type="text" name="publish_date" id="datepicker"  data-validate="{required:true}">
                            </div>
                        </div>-->
                    </div>
                </fieldset>
                <fieldset style="display:none">             
                    <div class="field required" style="display:none">
                    <label class="label" for="product_id"><span></span></label>
                        <select name="product_id[]">
                        <option value="<?php echo $_item->getProductId(); ?>"><?php echo $_item->getProductId(); ?></option>
                        </select>
                    </div>
                    <div class="field required" style="display:none">
                    <label class="label" for="customer_id"><span></span></label>
                        <select name="product_id[]">
                        <option value="<?php echo $_item->getCustomerId(); ?>"><?php echo $customerId; ?></option>
                        </select>
                    </div>
                </fieldset>
            </fieldset>
            <?php } } ?>
        <div class="actions-toolbar">
            <div class="primary">
                <button type="submit" class="action submit primary" title="Save"><span>Save</span></button>
            </div>
        </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    require(["jquery", "mage/calendar"], function($){
    $("#datepicker").calendar({
        showsTime: false,
        dateFormat: "M/d/Y",
        yearRange: "-120y:c+nn",
        maxDate: "-1d", changeMonth: true, changeYear: true})
    });
</script>

I attached an image while I am running the di:compile command.I have some errors there.

enter image description here

1 Answer 1

2

Step: 1

Please change the code in controller file as below

From:

protected $_postFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Softadroit\Prescription\Controller\Index $postFactory
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->_postFactory = $postFactory;
        return parent::__construct($context);
    }

To:

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Checkout\Model\CartFactory $cartFactory
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->cartFactory = $cartFactory;
        return parent::__construct($context);
    }

After that run php bin/magento setup:upgrade And then you can run php bin/magento setup:di:compile

Step 2:

Change the code in controller section

From =

if (!empty($post)) {
            $name   = $post['name'];
            $age    = $post['age'];
            $weight = $post['weight'];
            $height = $post['height'];
            $gender = $post['gender'];
            $product_id = $post['product_id'];
            $customer_id = $post['customer_id'];
        
            $model = $this->_objectManager->create('Softadroit\Prescription\Model\Prescription');           
            /* $model->setName($name);
            $model->setAge($age);
            $model->setWeight($weight);
            $model->setHeight($height);
            $model->setGender($gender);
            $model->setGender($product_id);
            $model->setGender($customer_id); */
            $model->setData([
                    "name" => $name,
                    "age" => $age,
                    "weight" => $weight,
                    "height" => $height,
                    "gender" => $gender,
                    "product_id" => $product_id,
                    "customer_id" => $customer_id
                    ]);
        
        /* echo "<pre>";
        print_r($model);
        die(); */
            
             $msg ="";
             $data =  $model->save();
            if($data != ''){ 
                //$model->save(); 
                $this->messageManager->addSuccessMessage('Thanks your form is submitted successfully!');
            }else{ 
                $this->messageManager->addSuccessMessage('Error in submitting data. Please contact on this email!'); 
            } 
            echo $msg; 
            
            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/prescription/index');
            return $resultRedirect;
        }

To=

if (!empty($post)) {
   foreach($post['name'] as $key => $name) {
            $age    = $post['age'][$key];
            $weight = $post['weight'][$key];
            $height = $post['height'][$key];
            $gender = $post['gender'][$key];
            $product_id = $post['product_id'][$key];
            $customer_id = $post['customer_id'][$key];
        
            $model = $this->_objectManager->create('Softadroit\Prescription\Model\Prescription');           
            
            $model->setData([
                    "name" => $name,
                    "age" => $age,
                    "weight" => $weight,
                    "height" => $height,
                    "gender" => $gender,
                    "product_id" => $product_id,
                    "customer_id" => $customer_id
                    ]);

             $msg ="";
             $data =  $model->save();
            if($data != ''){ 
                //$model->save(); 
                $this->messageManager->addSuccessMessage('Thanks your form is submitted successfully!');
            }else{ 
                $this->messageManager->addSuccessMessage('Error in submitting data. Please contact on this email!'); 
            } 
}
            echo $msg; 

            $cartObject = $this->cartFactory->create()->truncate();
            $cartObject->saveQuote();
            
            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/prescription/index');
            return $resultRedirect;
        }
7
  • I found this error while submitting form **Exception #0 (Exception): Notice: Array to string conversion ** Commented Aug 24, 2020 at 15:53
  • 1
    Data is echo in array but not get stored in db. Please have a look on the code Commented Aug 24, 2020 at 16:22
  • 2
    every attribute as an array. like $post['name'] is an array so you have to use foreach for every data. Commented Aug 24, 2020 at 17:25
  • I want to clear quote after submitting this form so what I have to do? Commented Aug 25, 2020 at 10:04
  • 1
    Yes, I have updated the above answer. Please try. Commented Aug 25, 2020 at 10:19

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.