3

I am trying to import some reviews programatically. I am using the following code:

$review = Mage::getModel('review/review');
$review->setEntityPkValue($product->getId());
$review->setStatusId(1);
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId(1);                                      
$review->setStoreId(Mage::app()->getStore()->getId());                    
$review->setStatusId(1);
$review->setCustomerId(NULL);
$review->setNickname("nickname");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));
$review->setCreatedAt(Mage::getModel('core/date')->gmtTimestamp($date));
$review->save();
$review->aggregate();

$date is a timestamp of the date of the review. The problem is that my reviews are being imported at the current date. How can I set the created_at parameter?

1 Answer 1

4

The problem is that the _beforeSave on the Mage_Review_Model_Resource_Review sets the created_at value when there is no id for the review, so for new reviews.

protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
    if (!$object->getId()) {
        $object->setCreatedAt(Mage::getSingleton('core/date')->gmtDate());
    }
    if ($object->hasData('stores') && is_array($object->getStores())) {
        $stores = $object->getStores();
        $stores[] = 0;
        $object->setStores($stores);
    } elseif ($object->hasData('stores')) {
        $object->setStores(array($object->getStores(), 0));
    }
    return $this;
}

You could do one of the following:

  1. Rewrite the resource and change the function,
  2. Save first and then save again
  3. There may be another option that I cannot think of at the moment :(
1
  • Thanks for the heads up, I am doing a one time import and so can just go into the core comment out that line and uncomment it when I am done. May 21, 2014 at 8:05

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.