Magento – Fatal error: Call to a member function beginTransaction() on a non-object

contact-usmagento-1.8php-5.4

hi all I am new to magento, I have a custom module here I save my form details but it's return an error

Fatal error: Call to a member function beginTransaction() on a non-object

my code is:

public function postAction()
    {

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

        $validate = $this->validate();

        if($validate === true){

                try {

                    $first_name = $this->getRequest()->getPost('full-name');
                    $email = $this->getRequest()->getPost('email');
                    $telephone = $this->getRequest()->getPost('telephone');
                    $comment = $this->getRequest()->getPost('comment');

                    Mage::log($first_name);

                    $contact = Mage::getModel('contactpro/contactpro');
                    $contact->setData('name', $first_name);
                    $contact->setData('email', $email);
                    $contact->setData('telephone', $telephone);
                    $contact->setData('comment', $comment);
                    $contact->save();

                    //$this->sendEmail();

                    $session->addSuccess($this->__('Contact has been accepted for moderation.'));

                    $pageThanks = Mage::getStoreConfig('contactpro/settings/page_thanks',$this->_getStore());
                    $this->_redirect($pageThanks);

                } catch (Exception $e){
                    Mage::getSingleton('customer/session')->addError($e->getMessage());
                    Mage::getSingleton('customer/session')->settestData($this->getRequest()->getPost());
                }


        }else{
            if (is_array($validate)) {
                foreach ($validate as $errorMessage) {
                    $session->addError($errorMessage);
                }
            } else {
                $session->addError($this->__('Unable to post the contact.'));
            }
            $this->_redirect('contactpro');
        }
    }

Can any one tell me where I went wrong? or suggest me.

thanks.

Best Answer

The beginTransaction method is called when saving an object. See Mage_Core_Model_Abstract::save():

...
$this->_getResource()->beginTransaction();
...

The error means _getResource returns something else than an object (probably null or false).
Make sure this class exists [Namespace]_Contactpro_Model_Resource_Contactpro.
It should look something like this

class [Namespace]_Contactpro_Model_Resource_Contactpro extends Mage_Core_Model_Resource_Db_Abstract {
    public function _construct() {
        $this->_init('contactpro/contactpro', 'entity_id'); //entity_id is the primary key in the contactpro table. If your key is different change it here
    }
}
Related Topic