Saturday, February 27, 2010

IPv6 for online customers

Problem : how to make Magento show the ipv6 ip address on online customers.
See full text here http://www.magentocommerce.com/boards/viewthread/79160/

Possible (untested) solution. If someone tests it please tell me the results also:

In theory you cannot do this, because in the database the fields for ip address are 'bigint' (see tables that start with log_visitor_)
All the ip addresses stored in this table 'suffer' a modification with ip2long() method. Unfortunately php does not support IPv6 (at least that's what I know. If I'm wrong someone please correct me).
Enough with the problems.
Here is what you can try.
Change the type of the columns for ip addresses from bigint to varchar.
In app/code/core/Mage/Log/Model/Visitor.php look for these lines:
$this->addData(array(
'server_addr'           => $helper->getServerAddr(true),
'remote_addr'           => $helper->getRemoteAddr(true), 
....
and remove the 'true' parameters from the functions.
$this->addData(array(
'server_addr'           => $helper->getServerAddr(),
'remote_addr'           => $helper->getRemoteAddr(), 
This should save the ip addresses as strings.

We are half way there. Now all you have to do is make sure that the ip addresses are rendered in the back-end grid you need to change the \app\code\core\Mage\Adminhtml\Block\Customer\Online\Grid\Renderer\Ip.php file.
Change this
public function render(Varien_Object $row)
{
return long2ip($row->getData($this->getColumn()->getIndex()));
}

to
public function render(Varien_Object $row)
{
return $row->getData($this->getColumn()->getIndex());
}

or to this, for backwards compatibility (to see the previous saved ip addresses)
public function render(Varien_Object $row)
{
$val = $this->getColumn()->getIndex();
if (is_int($val)){
return long2ip($val);
}
return $val;
}

Clear the cache and test it.
I have no way of testing this but it seams to me that this is the way to go.

(Of course you don't need to modify the core files if you plan to upgrade, you can overwrite them the proper magento way ).

Monday, February 1, 2010

Save an object into the database with a specific id. (insert - not update)

Problem can be found here:

Possible solution:

The presence of an id means update and if the id is null it means is insert.
So when you try to insert and add an id Magento is actually trying to update the entity but the id you specify does not exist.
It's something similar to this:
UPDATE some_table SET some_field = some_value WHERE id = some_id; 
The sql goes through but nothing gets updated.
If you want to modify something (but I don't advice you to do this) you can change the Mage_Core_Model_Mysql4_Abstract and Mage_Core_Model_Abstract models (the save() method for both of them.)

If you look inside the save method of Mage_Core_Model_Mysql4_Abstract you will see that there's an 'if' statement
if (!is_null($object->getId())) {...}
This means exactly what I explained above.

Here is an idea on how you can make magento do an insert with a specified id.
Overwrite the Mage_Core_Model_Abstract::save() method to accept and other parameter.
Let's call it $alwaysInsert and set it's default value to false (so you will not interfere with the other objects that use save()).
Then pass the $alwaysInsert parameter to the save method from the resource.
Your new method could look like this:
public function save($alwaysInsert = false)
{
$this->_getResource()->beginTransaction();
try {
$this->_beforeSave();
$this->_getResource()->save($this, $alwaysInsert);
$this->_afterSave();

$this->_getResource()->commit();
}
catch (Exception $e){
$this->_getResource()->rollBack();
throw $e;
}
return $this;
}

In Mage_Core_Model_Mysql4_Abstract, change the method save() to accept 2 parameters.
It could look like this:
public function save(Mage_Core_Model_Abstract $object, bool $alwaysInsert)
{
if ($object->isDeleted()) {
return $this->delete($object);
}

$this->_beforeSave($object);
$this->_checkUnique($object);

if (!is_null($object->getId()) && !$alwaysInsert) {
$condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $object->getId());
$this->_getWriteAdapter()->update($this->getMainTable(), $this->_prepareDataForSave($object), $condition);
} else {
$this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object));
$object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable()));
}

$this->_afterSave($object);

return $this;
} 

WARNING:
There are 2 issues (at least that is all I can think about now)
1. I didn't test this. You can test it and tell everyone the results
2. I'm not sure how Magento behaves when extending an abstract class. Normally it shouldn't cause any problems, but make sure the class(es) you are working with extend the new class(es) that you just created (extensions of Mage_Core_Model_Abstract and Mage_Core_Model_Mysql4_Abstract not these classes themselves).

If instead of overwriting you decide to modify the core this should take care of these issues, but you are not suppose to modify the code.
I hope this works for you. If it doesn't I'm sorry.