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?

2
  • you can use exception handling (i.e, try, cache) Commented Jul 6, 2015 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
    Commented Jul 6, 2015 at 10:57

2 Answers 2

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;
}
1
  • The isPkAutoIncrement was exactly my issue too. Commented Feb 23, 2017 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');
}
11
  • How to debug an issue when there is no error and yet no data is saved after save() is called?
    – Yeo
    Commented Jul 6, 2015 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. Commented Jul 6, 2015 at 17:59
  • The data looks good. It has every field needed. I need to know why they still fail.
    – Yeo
    Commented Jul 6, 2015 at 18:09
  • Have you check that in database? Commented Jul 6, 2015 at 18:12
  • That's the first thing I do to check whether the data is saved or not
    – Yeo
    Commented Jul 6, 2015 at 19:58

Your Answer

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

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