Question

I was recently tasked with debugging some faulty magento reset password email code. I have, I think, found the culprit of the faulty behavior but I am really confused as to how to resolve it. Here is my log:

2015-04-23T15:38:02+00:00 DEBUG (7): site id is: 1
2015-04-23T15:38:02+00:00 DEBUG (7): loaded customer by email
2015-04-23T15:38:02+00:00 ERR (3): Recoverable Error: Object of class Mage_Customer_Model_Customer could not be converted to string  in /var/www/magento/apijson/customer/forgotPassword.php on line 30
2015-04-23T15:38:02+00:00 DEBUG (7): the customer is:
2015-04-23T15:38:02+00:00 DEBUG (7): new reset password link token generated
2015-04-23T15:38:02+00:00 DEBUG (7): resetToken is: 44bbce19e297c088c2bca9f97a21d020
2015-04-23T15:38:02+00:00 DEBUG (7): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'entity_id' cannot be null, query was: INSERT INTO `customer_entity_varchar` (`entity_type_id`,`entity_id`,`attribute_id`,`value`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UP DATE `value` = VALUES(`value`)

Here is the reset email password code:

  5 $customer = Mage::getModel("customer/customer");
  6 Mage::log("site id is: ".Mage::app()->getWebsite()->getId());
  7 $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
...
 26 if ($email) {
 27     try {
 28         $customer->loadByEmail($email);
 29         Mage::log("loaded customer by email");
 30         Mage::log("the customer is: ".$customer);
 31         $newResetPasswordLinkToken = Mage::helper('customer')->generateResetPasswordLinkToken();                                                                                                                                                                          
 32         Mage::log("new reset password link token generated");
 33         Mage::log("resetToken is: ".$newResetPasswordLinkToken);
 34         $customer->changeResetPasswordLinkToken($newResetPasswordLinkToken);
 35         Mage::log("rest password link token changed");
 36         $customer->sendPasswordResetConfirmationEmail();
 37         Mage::log("password reset confirmation email sent");                                                                                                                                                                                                              
 38     } catch(Exception $e) {
 39         Mage::log($e->getMessage());                                                                                                                                                                                                                                      
 40     }

From the log I know the code fails when it gets to the changeResetPasswordLinkToken function. However I don't understand why. I've looked online and googled a lot for the last couple of days but it seems I have stumbled onto a very unique issue. I did find this post that had an answer to my issue but unfortunately it did not work for me.

So my question for anyone who views this post is. Do you know what's causing the issue, and why changeResetPasswordLinkToken is failing? If there is a question similar to this one that you think will be of use please provide link since anything helps at this point. Thanks everyone.

Was it helpful?

Solution

I think what is happening here is the load of the customer is failing.

I would check that this customer email address is really attached to a customer and also update you log for customer to something like:

Mage::log("the customer is: ".$customer->getId());

This will show you quickly if the load fails. If the load does fail then of course setting the password token will not work.

OTHER TIPS

To help you resolve this problem I need more details.

The method $customer->changeResetPasswordLinkToken($newResetPasswordLinkToken); calls same method from resource model. In resource model we see two rows, which most probably cause an issue:

 $this->saveAttribute($customer, 'rp_token');
 $this->saveAttribute($customer, 'rp_token_created_at');

The most probably issue is in the first row. I tried to look at customer_entity_varchar and find any values of attribute rp_token. But there were no any values with such attribute_id (attribute_id I got from eav_attribute).

Most probably Magento tries to change unexisted attribute value. To determine the issue try to put Mage::log() everywhere, as you did it before. And show us result.

Try this code:

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customerdata['email']);

if ($customer->getId()) {
    $customerId = $customer->getId();
    $customer = Mage::getModel('customer/customer')->load($customerId);
    // generate a new password
    $newPassword = $customer->generatePassword();
    $customer->changePassword($newPassword);
} else {
    // do something here for existing customers
}

try {
    $customer->save();
    $customer->setConfirmation(null);
    $customer->save();

    // save successful, send new password
    // uncomment this to send the email to the customer
    // $customer->sendPasswordReminderEmail();
} catch (Exception $e) {
    echo $e->getMessage();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top