Review – How to Set the Date of a Review Programmatically

review

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?

Best Answer

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 :(
Related Topic