0

I am creating custom module ,whose resource model is mysql4.In mysql4 file i am trying to call _beforeSave but

it show error 
beforeSave() should be compatible with that of Mage_Core_Model_Resource_Db_Abstract::_beforeSave()

here config.xml and mysql4 file are Vendor.php

config xml....

<?xml version="1.0" ?>
<config>
        <modules>
            <Amit_Vendor>
                <version>0.1.1</version>
            </Amit_Vendor>
        </modules>
    <global>    
    <!-- define blocks -->
        <blocks>
            <vendor>
                <class>Amit_Vendor_Block</class>
            </vendor>
        </blocks>
    <!-- define helper -->
        <helpers>
            <vendor>
                <class>Amit_Vendor_Helper</class>
            </vendor>   
        </helpers>
         <models>
            <vendor>
                <class>Amit_Vendor_Model</class>
                <resourceModel>vendor_mysql4</resourceModel>
            </vendor>
            <vendor_mysql4>
                <class>Amit_Vendor_Model_Mysql4</class>
                    <entities>
                        <vendor>
                            <table>vendor_user</table>
                        </vendor>
                        <vendordetails>
                            <table>vendor_marchent_details</table>
                        </vendordetails>
                    </entities>
            </vendor_mysql4>
        </models>
        <resources>
                <vendor_setup>
                    <setup>
                        <module>Amit_Vendor</module>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </vendor_setup>
                <vendor_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </vendor_write>
                <vendor_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </vendor_read>
            </resources>
    </global>
    <admin>
        <routers>
            <vendor>
                <use>admin</use>
                <args>
                    <module>Amit_Vendor</module>
                    <frontName>vendor</frontName>
                </args>
            </vendor>
        </routers>
    </admin>
    <frontend>
    <events>
            <controller_action_layout_load_before>
             <observers>
                <vendor_is_logged_in_check>
                    <class>vendor/observer</class>
                    <method>addVendorlaycheck</method>
                </vendor_is_logged_in_check>
             </observers>
            </controller_action_layout_load_before>
        </events>
        <routers>
            <vendor>
                <use>standard</use>
                <args>
                  <module>Amit_Vendor</module>
                  <frontName>vendor</frontName>
                </args>
            </vendor>
        </routers>
            <layout>
                <updates>
                    <vendor>
                        <file>vendor.xml</file>
                    </vendor>
                </updates>
            </layout>
    </frontend>

    <frontend>

    </frontend>
</config>


<?php
class Amit_Vendor_Model_Mysql4_Vendor extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {   
        $this->_init('vendor/vendor', 'vendor_id');
    }


    /**
     * Check customer by id
     *
     * @param int $customerId
     * @return bool
     */
    public function checkVendorId($vendorId)
    {
        $adapter = $this->_getReadAdapter();
        $bind    = array('vendor_id' => (int)$vendorId);
        $select  = $adapter->select()
            ->from($this->getTable('vendor/vendor'), 'vendor_id')
            ->where('vendor_id = :vendor_id')
            ->limit(1);

        $result = $adapter->fetchOne($select, $bind);
        if ($result) {
            return true;
        }
        return false;
    }
    /* check vendor email id exits or not
    * if not exits then thrown exception
    */
    protected function _beforeSave(Varien_Object $vendor){

        parent::_beforeSave($vendor);
        if(!$vendor->getEmail()){
            throw Mage::Exception('Bluehorse_Vendor',Mage::helper('vendor')->__('Vendor email is required'));
        }
        if(!$vendor->getUserName()){
            throw Mage::Exception('Bluehorse_Vendor',Mage::helper('vendor')->__('Vendor User is required'));
        }
        $this->checkforBefore($vendor);
        $adapter=$this->_getWriteAdapter();
        $bind=array('email'=>$vendor->getEmail());
         $select = $adapter->select()
            ->from($this->getTable('vendor/vendor'), array('email'))
            ->where('email = :email');

            if ($vendor->getVendorId()) {
            $bind['vendor_id'] = (int)$vendor->getVendorId();
            $select->where('vendor_id != :vendor_id');
        }

        $result = $adapter->fetchOne($select, $bind);
        if ($result) {
            throw Mage::exception(
                'Bluehorse_Vendor', Mage::helper('vendor')->__('This Vendor email already exists'),
                Bluehorse_Vendor_Model_Vendor::EXCEPTION_EMAIL_EXISTS
            );
        }
         return $this;

    }
    protected function checkforBefore(Varien_Object $vendor){

        $adapter=$this->_getWriteAdapter();
        $bind=array('user_name'=>$vendor->getUserName());
         $select = $adapter->select()
            ->from($this->getTable('vendor/vendor'), array('user_name'))
            ->where('user_name = :user_name');

            if ($vendor->getVendorId()) {
            $bind['vendor_id'] = (int)$vendor->getVendorId();
            $select->where('vendor_id != :vendor_id');
        }

        $result = $adapter->fetchOne($select, $bind);
        if ($result) {
            throw Mage::exception(
                'Bluehorse_Vendor', Mage::helper('vendor')->__('This Vendor UserName already exists'),
                Bluehorse_Vendor_Model_Vendor::EXCEPTION_USERNAME_EXISTS
            );
        }
     return $this;
    }
    public function loadByEmail(Bluehorse_Vendor_Model_Vendor $vendor, $email){
        $adpter=$this->_getReadAdapter();
        $bind=array('email'=>$email);
        $select=$adpter->select()
        ->from($this->getTable('vendor/vendor'), array('vendor_id'))
         ->where('email = :email');
          $vendorId = $adpter->fetchOne($select, $bind);

        if ($vendorId) {
            $this->load($vendor,$vendorId);
        } else {
            $vendor->setData(array());
        }

        return $this;
    }

}

Please help me.....

3

That's happened because if you rewrite some method of parent class it should contain the same amount of entered parameters and parameters should be the same type. In your case _beforeSave has one input parameter and its type is Mage_Core_Model_Abstract but you use Varien_Object. PHP doesn't allow you to do that. So instead of

protected function _beforeSave(Varien_Object $vendor)

Use

protected function _beforeSave(Mage_Core_Model_Abstract $vendor)
  • Oleksii now show error Declaration of Amit_Vendor_Model_Mysql4_Vendor::_beforeSave() should be compatible with that of Mage_Core_Model_Resource_Db_Abstract::_beforeSave() – Amit Bera Feb 3 '14 at 9:52
  • Can you update the question with changed code? – oleksii.svarychevskyi Feb 3 '14 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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