Reputation: 99
I created an attribute for customer entity like this
$setup->addAttribute( 'customer', 'created_time', array(
'type' => 'datetime',
'input' => 'date',
'backend' => 'eav/entity_attribute_backend_datetime',
'label' => 'Created Time',`enter code here`
'required' => false,
'visible' => true,
)
);
and when I am trying to insert the date as yyyy-mm-dd H:i:s
format. It is getting inserted in yyyy-dd-mm
format. I don't know why this is happening. When I tried to insert date as yyyy-dd-mm H:i:s then it got inserted properly in the format yyyy-mm-dd H:i:s
.
But if I update any info of customer again the value for attribute changes for that customer to yyyy-dd-mm again.
Don't know why this is happening.
Upvotes: 2
Views: 2756
Reputation: 41
May be this can help someone.
This method can solve the problem in Magento 1.9.
In a few words, your just need rewrite Mage_Eav_Model_Entity_Attribute_Backend_Datetime model in your module and add your model name in ‘backend’ param.
1) Create model in your module: Path: yourCompany\yourModule\Model\Entity\Attribute\Backend\Datetime.php
class yourCompany_yourModule _Model_Entity_Attribute_Backend_Datetime extends Mage_Eav_Model_Entity_Attribute_Backend_Datetime
{
/**
* Prepare date for save in DB
*
* string format used from input fields (all date input fields need apply locale settings)
* int value can be declared in code (this meen whot we use valid date)
*
* @param string | int $date
* @return string
*/
public function formatDate($date)
{
if (empty($date)) {
return null;
}
// unix timestamp given - simply instantiate date object
if (preg_match('/^[0-9]+$/', $date)) {
$date = new Zend_Date((int)$date);
}
// international format
else if (preg_match('#^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$#', $date)) {
$zendDate = new Zend_Date();
$date = $zendDate->setIso($date);
}
// parse this date in current locale, do not apply GMT offset
else {
$date = Mage::app()->getLocale()->date($date,
Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
null, false
);
}
return $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
}
}
This model does only one thing, replaces the name of one method
Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
to another
Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
2) Add attribute
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->startSetup();
$setup->addAttribute( 'customer', 'created_time', array(
'type' => 'datetime',
'input' => ' datetime ',
'backend' => 'yourModelNamespace/entity_attribute_backend_datetime',
'label' => 'Created Time',
'required' => false,
'visible' => true,
)
);
$setup->endSetup();
3) For complete beginners. You can find yourModelNamespace here. Path: yourCompany\yourModule\etc\config.xml
<global>
<models>
<yourModelNamespace>
<class>yourCompany_yourModule_Model</class>
<resourceModel>yourModelNamespace_mysql4</resourceModel>
</yourModelNamespace>
</models>
</global>
Another method:
1) Copy file Mage\Eav\Model\Entity\Attribute\Backend\Datetime.php to local code pool Mage\Eav\Model\Entity\Attribute\Backend\Rightdatetime.php with new name. To avoid unpredictable problems.
2) Replace getDateFormat on getDateTimeFormat
3) In db in "eav_attribute" table update your attribute field "backend_model" eav/entity_attribute_backend_rightdatetime
Upvotes: 4