1

I Have Created a Custom Module. In This Module Have a Field Like created_time and update_time. How To Insert That Date In Database Custom Table?

Here Is My Code..

C:\xampp2\htdocs\mymagento\app\code\local\Rushikesh\Module\Block\Adminhtml\Module\Edit\Tab\Form.php

<?php

class Rushikesh_Module_Block_Adminhtml_Module_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('module_form', array('legend'=>Mage::helper('module')->__('Item information')));

        $fieldset->addField('title', 'text', array(
            'label'     => Mage::helper('module')->__('Title'),
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'title',
        ));

        /* $dateTimeFormatIso = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
        $fieldset->addField('date_and_time', 'datetime', array(
            'label'    => Mage::helper('module')->__('Date time'),
            'title'    => Mage::helper('module')->__('Date time'),
            'time'     => true,
            'name'     => 'date_and_time',
            'image'    => $this->getSkinUrl('images/grid-cal.gif'),
            'format'   => $dateTimeFormatIso,
            'required' => true,
        )); */

        $dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
        $fieldset->addField('created_time', 'date', array(
            'name'      => 'created_time',
            'label'     => Mage::helper('module')->__('Created Time'),
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
            'format'    => $dateFormatIso,
            'disabled'  => $isElementDisabled,
            'class'     => 'validate-date validate-date-range date-range-custom_theme-from'
        ));

        $dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
        $fieldset->addField('update_time', 'date', array(
            'name'      => 'update_time',
            'label'     => Mage::helper('module')->__('Update Time'),
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
            'format'    => $dateFormatIso,
            'disabled'  => $isElementDisabled,
            'class'     => 'validate-date validate-date-range date-range-custom_theme-from'
        ));

        $fieldset->addField('status', 'select', array(
            'label'     => Mage::helper('module')->__('Status'),
            'name'      => 'status',
            'values'    => array(
                array(
                    'value'     => 1,
                    'label'     => Mage::helper('module')->__('Active'),
                ),

                array(
                    'value'     => 0,
                    'label'     => Mage::helper('module')->__('Inactive'),
                ),
            ),
        ));

        $fieldset->addField('content', 'editor', array(
            'name'      => 'content',
            'label'     => Mage::helper('module')->__('Content'),
            'title'     => Mage::helper('module')->__('Content'),
            'style'     => 'width:98%; height:400px;',
            'wysiwyg'   => false,
            'required'  => true,
        ));

        if ( Mage::getSingleton('adminhtml/session')->getModuleData() )
        {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getModuleData());
            Mage::getSingleton('adminhtml/session')->setModuleData(null);
        } elseif ( Mage::registry('module_data') ) {
            $form->setValues(Mage::registry('module_data')->getData());
        }
        return parent::_prepareForm();
    }
}

C:\xampp2\htdocs\mymagento\app\code\local\Rushikesh\Module\controllers\Adminhtml\ModuleController.php

<?php

class Rushikesh_Module_Adminhtml_ModuleController extends Mage_Adminhtml_Controller_action
{

    protected function _initAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('module/items')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
        return $this;
    }   

    public function indexAction() {
        $this->_initAction();       
        $this->_addContent($this->getLayout()->createBlock('module/adminhtml_module'));
        $this->renderLayout();
    }

    public function editAction()
    {
        $moduleId     = $this->getRequest()->getParam('id');
        $moduleModel  = Mage::getModel('module/module')->load($moduleId);

        if ($moduleModel->getId() || $moduleId == 0) {

            Mage::register('module_data', $moduleModel);

            $this->loadLayout();
            $this->_setActiveMenu('module/items');

            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

            $this->_addContent($this->getLayout()->createBlock('module/adminhtml_module_edit'))
                 ->_addLeft($this->getLayout()->createBlock('module/adminhtml_module_edit_tabs'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('module')->__('Item does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function newAction()
    {
        $this->_forward('edit');
    }

    public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();
                $moduleModel = Mage::getModel('module/module');

                $moduleModel->setId($this->getRequest()->getParam('id'))
                    ->setTitle($postData['title'])
                    ->setContent($postData['content'])
                    ->setStatus($postData['status'])
                    ->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setModuleData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setModuleData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction()
    {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $moduleModel = Mage::getModel('module/module');

                $moduleModel->setId($this->getRequest()->getParam('id'))
                    ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }
}

1 Answer 1

1

update your saveAction as below

public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();
                $moduleModel = Mage::getModel('module/module');

                $moduleModel->setId($this->getRequest()->getParam('id'))
                    ->setTitle($postData['title'])
                    ->setContent($postData['content'])
                    ->setStatus($postData['status'])
                    ->setUpdateTime(date("Y-m-d H:i:s"));
                    if(!$this->getRequest()->getParam('id')){
                        $moduleModel->setCreatedTime(date("Y-m-d H:i:s"));
                    }
                    $moduleModel->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setModuleData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setModuleData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.