1

I'm trying to Import the product reviews, into my new magento site. For that When I create product review programmatically, I can't set my own date for createdAt field of product review.

When I'm looking into the core file, following function in app/code/core/Mage/Review/Model/Resource/Review.php set the created at date by itself

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;
    }

I'm running following code to create review programmatically

<?php
ini_set('memory_limit', '128M');
require_once 'app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(1); 
$date = gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 2014));
$review = Mage::getModel('review/review');
$review->setReviewId($review->getId());
$review->setEntityPkValue(1);//product id
$review->setStatusId(1);
$review->setTitle("mytitle"); 
$review->setDetail("mydetail");
$review->setEntityId(1);                                      
$review->setStoreId(Mage::app()->getStore()->getId());      //storeview               
$review->setStatusId(1); //approved
$review->setCustomerId(1);//null is for administrator
$review->setNickname("Menickname");
$review->setCreatedAt($date);
$review->setStores(array(1,3));                 
$review->save();
$review->aggregate();

How Can I set a date by myself? Is there any way?

SIBHI S
  • 2,055
  • 4
  • 28
  • 41
  • Use $date = data('now'); – Charlie Jun 30 '14 at 12:52
  • No, Actually $date variable is not a problem, I need to set date from csv file to import it, So I want to call setCreatedAt('1-1-2014') like this, But I could not call that function, Since it is called when saving review by magento itself. – SIBHI S Jun 30 '14 at 12:59

2 Answers2

10

I see 2 options here.
Option 1. Rewrite the method you mentioned and make it allow you own created at. Just make sure it does not interfere with the regular process of posting a comment. Try with something like this: (untested code)

protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
    if (!$object->getId() && !$object->getSkipCreatedAtSet()) { //add an other constraint here
        $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;
}

Then when creating your review programatically, add this in the code just before calling save.

$review->setSkipCreatedAtSet(true);

This should prevent the entering in the first if statement from beforeSave and it will use the value you set for created at.

Option 2. Since the created_at is set only when the $object does not have an id, you can try to save the object again. something like what you do, but after calling save do this. (untested code again)

$review->setCreatedAt(....);
$review->save();

But this may slow your process down because you call save twice.

I would go with the first approach. Seams cleaner and more flexible.

Marius
  • 197,939
  • 53
  • 422
  • 830
0

SIBHI S, if you set custom date when you create review pragmatically. You should be change table structure of review. There are set default value is CURRENT_TIMESTAMP of createAt field. You set it as As Define. That's it.

Supravat Mondal
  • 1,632
  • 1
  • 21
  • 29