Magento – Custom module form data is not saving in magento

formsmagento-1.8modulephp-5.4

I am new to magento, I have created custom module which is contactpro. in this I am getting data and receiving mail and redirecting page also, but data is not saving in my table.

my code is:

IndexController.php

public function postAction(){

        $session            = Mage::getSingleton('core/session');
        $data               = Mage::app()->getRequest()->getPost();
        $this->_data        = $data;

        $validate = $this->validate();

        if($validate === true){
            $session->addSuccess($this->__('Contact has been accepted for moderation.'));

                try {
                    $postData = $this->getRequest()->getPost();
                    Mage::log($postData);
                    Mage::log('================================');
                    $testModel = Mage::getModel('contactpro/contactpro');
                    Mage::log($testModel);
                    $testModel->setCreatedTime(Mage::getSingleton('core/date')->gmtDate());
                    $testModel->addData($postData)->setUpdateTime(Mage::getSingleton('core/date')->gmtDate())
                        ->setId($this->getRequest()->getParam('contact_pro_id'))
                        ->save();
                    Mage::getSingleton('adminhtml/session')->addSuccess('successfully saved');
                    Mage::getSingleton('adminhtml/session')->settestData(false);
                } catch (Exception $e){
                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                    Mage::getSingleton('adminhtml/session')->settestData($this->getRequest()->getPost());
                }

            //$this->sendEmail();
            $pageThanks = Mage::getStoreConfig('contactpro/settings/page_thanks',$this->_getStore());
            $this->_redirect($pageThanks);
        }else{
            if (is_array($validate)) {
                foreach ($validate as $errorMessage) {
                    $session->addError($errorMessage);
                }
            } else {
                $session->addError($this->__('Unable to post the contact.'));
            }
            $this->_redirect('contactpro');
        }
    }

and I have receive system.log file

2014-02-24T12:09:39+00:00 DEBUG (7): Array
(
    [full-name] => Magento
    [email] => test.t@gmail.com
    [telephone] => 123456
    [comment] => Test           
)

2014-02-24T12:09:39+00:00 DEBUG (7): ================================
2014-02-24T12:09:39+00:00 DEBUG (7): Easylife_Contactpro_Model_contactpro Object
(
    [_eventPrefix:protected] => core_abstract
    [_eventObject:protected] => object
    [_resourceName:protected] => contactpro/contactpro
    [_resource:protected] => 
    [_resourceCollectionName:protected] => contactpro/contactpro_collection
    [_cacheTag:protected] => 
    [_dataSaveAllowed:protected] => 1
    [_isObjectNew:protected] => 
    [_data:protected] => Array
        (
        )

    [_hasDataChanges:protected] => 
    [_origData:protected] => 
    [_idFieldName:protected] => 
    [_isDeleted:protected] => 
    [_oldFieldsMap:protected] => Array
        (
        )

    [_syncFieldsMap:protected] => Array
        (
        )

)

table structure is:

<?php

$installer = $this;

$installer->startSetup();

$installer->run("

-- DROP TABLE IF EXISTS {$this->getTable('contact_pro')};
CREATE TABLE {$this->getTable('contact_pro')} (
  `contact_pro_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `telephone` varchar(50) NOT NULL,
  `comment` varchar(50) NOT NULL,
  PRIMARY KEY (`contact_pro_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");

$installer->endSetup(); 

magento\app\code\local\Easylife\Contactpro\Model\Contactpro.php

 <?php

    class Easylife_Contactpro_Model_Contactpro extends Mage_Core_Model_Abstract
    {
        public function _construct()
        {
            parent::_construct();
            $this->_init('Contactpro/Contactpro');
        }
    }

magento\app\code\local\Easylife\Contactpro\Model\Resource\Contactpro.php

<?php

class Easylife_Contactpro_Model_Resource_Contactpro extends Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
        $this->_init('contactpro/contactpro','contactpro_pro_id');
    }

}

magento\app\code\local\Easylife\Contactpro\Model\Resource\Contactpro\Contactpro.php

<?php

class Easylife_Contactpro_Model_Resource_Contactpro_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{



    protected $_intervals = null;

    public function _construct()
    {
        $this->_init('contactpro/contactpro');
    }


    public function getIntervals()
    {
        if (!is_null($this->_intervals)) {
            return $this->_intervals;
        }

        return 1;
    }

}

can any one tell me where I went wrong?

Best Answer

Please change in the parameters... and save data function

$session            = Mage::getSingleton('core/session');
        $data               = Mage::app()->getRequest()->getPost();
        $this->_data        = $data;

        $validate = $this->validate();

        if($validate === true){
            $session->addSuccess($this->__('Contact has been accepted for moderation.'));

                try {

                $contactproModel = Mage::getModel('contactpro/contactpro');
                 $contactproModel->load($this->getRequest()->getParam('contact_pro_id'));
               if(!empty($data) and $contactproModel->getId() ){
                $contactproModel
                    ->setName($data['full-name'])
                    ->setEmail($data['email'])
                    ->setTelephone($data['telephone'])
                    ->setComment($data['comment'])
                    ->save();
               }
               elseif(!empty($data)){
                    Mage::getModel('contactpro/contactpro')
                    ->setName($data['full-name'])
                    ->setEmail($data['email'])
                    ->setTelephone($data['telephone'])
                    ->setComment($data['comment'])
                    ->save();
               }
                    Mage::getSingleton('core/session')->addSuccess('successfully saved');
                    Mage::getSingleton('core/session')->settestData(false);
                } catch (Exception $e){
                    Mage::getSingleton('core/session')->addError($e->getMessage());
                    Mage::getSingleton('core/session')->settestData($this->getRequest()->getPost());
                }

            //$this->sendEmail();
            $pageThanks = Mage::getStoreConfig('contactpro/settings/page_thanks',$this->_getStore());
            $this->_redirect($pageThanks);
        }else{
            if (is_array($validate)) {
                foreach ($validate as $errorMessage) {
                    $session->addError($errorMessage);
                }
            } else {
                $session->addError($this->__('Unable to post the contact.'));
            }
            $this->_redirect('contactpro');
        }
Related Topic