0

I tried to alter information from the table review_detail by loading the model, changing values and saving the model again, but being stuck.

The following code does not work:

$review=Mage::getModel('review/review')->load(12);
$review->setCustomerId(2)->save();

There is no error thrown, no error in the logs either.

  • use try {} catch {} for getting error – Amit Bera Sep 4 '15 at 9:36
  • I had same problem when I was missing ->setStoreId(yourstoreid) so please try this – Stefan Gregori Sep 4 '15 at 9:42
  • Thanks for your hints. I noticed that the problem occurs only with the column "customer_id", which is not saved. The other columns, such as "detail", "title" or "nickname" save without any problem. – Peter Bättig Sep 4 '15 at 10:50
0

In Mage/Review/Model/Resource/Review there exists the function _afterSave which seems to set the customer ID after saving the model. Thus overwriting the changes made before saving it.

I decided to solve my problem by creating a helper function to directly write the customer ID to the table review_detail:

public function saveCustomerIdToReview($reviewId,$customerId){
    $resource=Mage::getSingleton('core/resource');
    $writeConnection=$resource->getConnection('core_write');
    $tableName = $resource->getTableName('review/review_detail');
    $query = "Update {$tableName} SET customer_id = '{$customerId}' WHERE review_id = " . (int)$reviewId;
    try{
        $writeConnection->query($query);
    } catch (Exception $ex){
        $this->logerror($ex->getMessage());
    }
}

One other solution would have been rewriting the _afterSave method in Mage_Review_Model_Resource_Review.

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.