2

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?

  • you can use exception handling (i.e, try, cache) – MeenakshiSundaram R Jul 6 '15 at 9:49
  • @MeenakshiSundaramR Thanks for the help. I tried to use the php exception handler and it works. try {} catch (Exception $e) {$e->getMessage();} will get the error message. – Yeo Jul 6 '15 at 10:57
2

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;
}
  • The isPkAutoIncrement was exactly my issue too. – Matt Cosentino Feb 23 '17 at 23:01
1

As we disused in the comment.

Use exception handler to catch the error.(i.e., try, catch)

$transaction = App::getModel('mygateway/transaction');
$transaction->setOrderId($order_id);
$transaction->setTransactionId($transaction_id);
try{ 
   $transaction->save();
}
catch(Exception $error){
   Mage::log($error->getMessage(), null, 'yourlogfile.log');
}
  • How to debug an issue when there is no error and yet no data is saved after save() is called? – Yeo Jul 6 '15 at 17:56
  • Get data before save $transaction->get data(); by using this you can check your data is set to that variable or not. – MeenakshiSundaram R Jul 6 '15 at 17:59
  • The data looks good. It has every field needed. I need to know why they still fail. – Yeo Jul 6 '15 at 18:09
  • Have you check that in database? – MeenakshiSundaram R Jul 6 '15 at 18:12
  • That's the first thing I do to check whether the data is saved or not – Yeo Jul 6 '15 at 19:58

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.