1

I need to save form data in table . I need to know better way. Is it better to store from controller. i have tried using following code but no success

form

<form id="orderapproval-item-form" class="orderapproval-hidden" action="<?php echo $this->getUrl('orderapproval/customercart/remarksdecline');?>" method="post">
<input type="text" name="message" class="orderapproval-disabled" value="<?php echo $this->__('Enter decline reason'); ?>" id="decline" />
<input type="submit" value="<?php echo $this->__('Decline'); ?>" onclick="namet()" />

controller

    public function remarksdeclineAction()
    {
       $message =  $this->getRequest()->getParam('message');
       $model =Mage::getModel('orderapproval/sales_flat_quote_item');
       $model->setItemcomment( $message );
       $model->save(); // save data

    }

Model/Orderapproval.php

<?php
class ZetaPrints_OrderApproval_Model_OrderApproval extends Mage_Core_Model_Abstract
{
        public function _construct()
    {
        $this->_init('orderapproval/sales_flat_quote_item');
    }
}

Model/Resource/Orderapproval.php

<?php
class ZetaPrints_OrderApproval_Model_Resource_OrderApproval extends Mage_Core_Model_Resource_Db_Abstract
{
        public function _construct()
    {
        $this->_init('orderapproval/sales_flat_quote_item', 'item_id');
    }
}

Model/Resource/Orderapproval/Collection.php

<?php
class ZetaPrints_OrderApproval_Model_Resource_OrderApproval_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
        protected function _constuct(){
 $this->_init('orderapproval/sales_flat_quote_item'); 
 }
}

config.xml

<?xml version="1.0" ?>
<config>
  <modules>
    <ZetaPrints_OrderApproval>
      <version>3</version>
    </ZetaPrints_OrderApproval>
  </modules>

  <global>
    <models>
      <orderapproval>
        <class>ZetaPrints_OrderApproval_Model</class>
        <resourceModel>zetaprints_orderapproval_resource</resourceModel>
      </orderapproval>

      <ZetaPrints_OrderApproval_resource>
         <class>ZetaPrints_OrderApproval_Model_Resource</class>
         <entities>
             <sales_flat_quote_item>
               <table>sales_flat_quote_item</table>
             </sales_flat_quote_item>
          </entities>
    </ZetaPrints_OrderApproval_resource>

      <sales>
        <rewrite>
          <quote>ZetaPrints_OrderApproval_Model_Quote</quote>
        </rewrite>
      </sales>
    </models>

    <blocks>
      <orderapproval>
        <class>ZetaPrints_OrderApproval_Block</class>
      </orderapproval>
    </blocks>

    <helpers>
      <orderapproval>
        <class>ZetaPrints_OrderApproval_Helper</class>
      </orderapproval>
    </helpers>

    <resources>
      <orderapproval_setup>
        <setup>
          <module>ZetaPrints_OrderApproval</module>
          <class>Mage_Eav_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>Core_setup</use>
        </connection>
      </orderapproval_setup>

      <orderapproval_write>
        <connection><use>Core_write</use></connection>
      </orderapproval_write>

      <orderapproval_read>
        <connection><use>Core_read</use></connection>
      </orderapproval_read>
    </resources>

its my folder name and file name of model folder

**

  1. ZetaPrints/OrderApproval/Model/Orderapproval.php

**

  1. ZetaPrints/OrderApproval/Model/Resource/Orderapproval.php
  2. ZetaPrints/OrderApproval/Model/Resource/Orderapproval/Connection.php
2
  • mr piyush please reply if you know Dec 28, 2016 at 7:20
  • i have updated my question please check and reply me Jan 2, 2017 at 6:06

1 Answer 1

3

You tried magento direct sql query for insert data in sales_quote_item_order.

As, magento is using ORM and MVC,so you need to create a model for that table .

Then using model save the form data.

So, you need add

  • model and
  • resource model
  • database connection

add config.xml

Example:

<models>
    <[ModelPrefix]>
        <class>[YourcompanyName]_[moduleName]_Model</class>
        <resourceModel>[ModelPrefix]_resource</resourceModel>
    </[ModelPrefix]>
    <[ModelPrefix]_resource>
        <class>[YourcompanyName]_[moduleName]_Model_Resource</class>
        <entities>
            <[ModelPrefix]>
                <table>sales_quote_item_order</table> <!-- table Name -->
            </[ModelPrefix]>
        </entities>
    </[ModelPrefix]_resource>
</models>
<resources>
    <[ModelPrefix]_setup>
        <setup>
            <module>[YourcompanyName]_[moduleName]</module>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </[ModelPrefix]_setup>
    <[ModelPrefix]_read>
        <connection>
            <use>core_read</use>
        </connection>
    </[ModelPrefix]_read>
    <[ModelPrefix]_write>
        <connection>
            <use>core_write</use>
        </connection>
    </[ModelPrefix]_write>
</resources>

and also it related Model classes..

  • Model: [YourcompanyName]_[moduleName]_Model_[moduleName]

  • Resource class : [YourcompanyName]_[moduleName]_Model_Resource_[moduleName]

  • Collection class : [YourcompanyName]_[moduleName]_Model_Resource_[moduleName]_Collection

Checkout how to create model of table

For save data use model and setter function at your contorller

$model =Mage::getModel('[ModelPrefix]/[ModelPrefix]>');
$model->setItemcomment($message);
$model->save(); // save data

edit:

You have done lot of issue in folder structure and file name

All files and folder which you will given u name OrderApproval it should be Orderapproval . A should lower case.

compare config.xml with :

<modules>
    <ZetaPrints_Orderapproval>
      <version>3.0.0</version>
    </ZetaPrints_Orderapproval>
  </modules>
  <global>

    <models>
      <orderapproval>
        <class>ZetaPrints_Orderapproval_Model</class>
        <resourceModel>orderapproval_resource</resourceModel>
      </orderapproval>
      <orderapproval_resource>
        <class>ZetaPrints_Orderapproval_Model_Resource</class>
        <entities>        
              <sales_quote_item_order>
                <table>sales_quote_item_order</table>
              </sales_quote_item_order>
        </entities>
      </orderapproval_resource>
    </models>
    <resources>
      <orderapproval_setup>
        <setup>
          <module>ZetaPrints_Orderapproval</module>
          <class>Mage_Eav_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </orderapproval_setup>
      <orderapproval_write>
        <connection>
          <use>core_write</use>
        </connection>
      </orderapproval_write>
      <orderapproval_read>
        <connection>
          <use>core_read</use>
        </connection>
      </orderapproval_read>
    </resources>
  </global>
21
  • where should i write database query Dec 28, 2016 at 9:26
  • As you have MVC and magento, so you donot need to rewtite sql qurry using Model you can save data
    – Amit Bera
    Dec 28, 2016 at 9:45
  • okay i have done as per you please let me check Dec 28, 2016 at 9:46
  • but what should i write in controller Dec 28, 2016 at 9:47
  • i have added in controller ... $model =Mage::getModel('OrderApproval/OrderApproval'); $model->setItemcomment($message); $model->save(); // save data Dec 28, 2016 at 9:50

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.