0

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>

setup file for table

<?php
$installer = $this;
$installer->startSetup();
$installer->run("
ALTER TABLE `{$installer->getTable('sales/flat_item')}` 
ADD `itemcomment` VARCHAR( 255 ) NOT NULL;

");
$installer->endSetup();

its my folder name and file name of model folder

**

ZetaPrints/OrderApproval/Model/Orderapproval.php
**

ZetaPrints/OrderApproval/Model/Resource/Orderapproval.php
ZetaPrints/OrderApproval/Model/Resource/Orderapproval/Connection.php

i have updated my question any one can reply me

3
  • anyone can help me. last two week i am searching for this question someone replied but i did not get solution. Jan 2, 2017 at 9:10
  • can you share your table fields? Have your create via script or not?
    – Abdul
    Jan 2, 2017 at 10:09
  • no i have created manually not from script Jan 2, 2017 at 10:12

1 Answer 1

2

Make changes in your Model/Orderapproval.php file:

<?php
class ZetaPrints_OrderApproval_Model_OrderApproval extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('orderapproval/sales_quote_item_order'); //remove item_id from here as it is already defined in your Model/Resource/Orderapproval.php file. Make sure this 'item_id is primary key of your table.
    }
}

Then in your controller file make changes as below:

public function remarksdeclineAction()
{
   $message =  $this->getRequest()->getParam('message');
   $model =Mage::getModel('orderapproval/sales_quote_item_order');
   $model->setItemId($item_id); //set your item_id to add the comment in particular row.
   $model->setItemcomment( $message );
   $model->save(); // save data
}

Also make sure that the name of the field in table is itemcomment and not item_comment, because in second case you need to save like this $model->setItemComment()

14
  • column name is itemcomment Jan 2, 2017 at 10:11
  • dude thanks for your reply but i am getting ...Fatal error: Uncaught Error: Call to a member function setItemId() on boolean Jan 2, 2017 at 10:14
  • ok can you share the setup file from which you have created the table ? I read in your comment that you have created it manually. Then what is the name of your primary key id in that table ? it should be item_id Jan 2, 2017 at 10:15
  • <?php $installer = $this; $installer->startSetup(); $installer->run(" ALTER TABLE {$installer->getTable('sales/quote_item')} ADD itemcomment VARCHAR( 255 ) NOT NULL; "); $installer->endSetup(); Jan 2, 2017 at 10:19
  • dude setup file i have updated in my question Jan 2, 2017 at 10:22

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.