Magento – Understanding Error in Model save

debuggingmodel

How can I debug or show the error trace of errors occur in calling Model->save()?

At the moment, I'm using logger to debug the execution.

$transaction = App::getModel('mygateway/transaction');
$transaction->setOrderId($order_id);
$transaction->setTransactionId($transaction_id);

Mage::log('saving', null, 'mygateway.log', true); 
$transaction->save();
Mage::log('saved', null, 'mygateway.log', true);

Is there something like: $transaction->getLastError()?

Update (2nd Question)

How to debug $transaction->save() when it is executed without error, and yet the data is not stored in the database?

Best Answer

Here's how I debug.

  1. Read at the source code.
  2. Overwrite your resource model class save method. And log at each conditions.

    class Awesome_Cool_Model_Resource_Payment
        extends Mage_Core_Model_Resource_Db_Abstract 
    {
        protected function _constructor(){
            $this->_init('awesome_cool/payment', 'txn_id');
        }
    
        public function save(Mage_Core_Model_Abstract $object){
            // Copy the parent code
            // https://github.com/OpenMage/magento-mirror/blob/1.9.1.1/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php#L416-L466
            // ..........................................
            // ..........................................
            // ..........................................
            // ..........................................
            // ..........................................
    
            // Log Everywhere
            // Mage::log('Hi5', null, 'mylog.log');
    
        }
    }
    
  3. Understand the code flow. (Why the code not executed at certain point)

In my case, My issue is for non increment primary key

solution:

protected function _constructor(){
    $this->_init('awesome_cool/payment', 'txn_id');
    $this->_isPkAutoIncrement = false;
}