Skip to main content
Commonmark migration
Source Link

You have a misconception in how Magento models work.

They are not "loaders" which just give you back a data object.

They represent the entity itself and load themselves (this is called "Active Record").

So you have a model $addressObject and load it twice, from different ids. The second load overwrites the data from the first load.

Your code only works because load() happens to return $this for convenience. In the end $billAddress, $shipAddress and $addressObject all hold the identical object. Objects in PHP are not copied when you assign them to a different variable, the variable only holds a reference to the actual object.

###Correct code:

Correct code:

$billId = 'VALID BILLING ADDRESS ID'; $shipAddress = Mage::getModel('customer/address')->load($shipId); $billAddress = Mage::getModel('customer/address')->load($billId); 

You have a misconception in how Magento models work.

They are not "loaders" which just give you back a data object.

They represent the entity itself and load themselves (this is called "Active Record").

So you have a model $addressObject and load it twice, from different ids. The second load overwrites the data from the first load.

Your code only works because load() happens to return $this for convenience. In the end $billAddress, $shipAddress and $addressObject all hold the identical object. Objects in PHP are not copied when you assign them to a different variable, the variable only holds a reference to the actual object.

###Correct code:

$billId = 'VALID BILLING ADDRESS ID'; $shipAddress = Mage::getModel('customer/address')->load($shipId); $billAddress = Mage::getModel('customer/address')->load($billId); 

You have a misconception in how Magento models work.

They are not "loaders" which just give you back a data object.

They represent the entity itself and load themselves (this is called "Active Record").

So you have a model $addressObject and load it twice, from different ids. The second load overwrites the data from the first load.

Your code only works because load() happens to return $this for convenience. In the end $billAddress, $shipAddress and $addressObject all hold the identical object. Objects in PHP are not copied when you assign them to a different variable, the variable only holds a reference to the actual object.

Correct code:

$billId = 'VALID BILLING ADDRESS ID'; $shipAddress = Mage::getModel('customer/address')->load($shipId); $billAddress = Mage::getModel('customer/address')->load($billId); 
Source Link
Fabian Schmengler
  • 66.2k
  • 25
  • 191
  • 422

You have a misconception in how Magento models work.

They are not "loaders" which just give you back a data object.

They represent the entity itself and load themselves (this is called "Active Record").

So you have a model $addressObject and load it twice, from different ids. The second load overwrites the data from the first load.

Your code only works because load() happens to return $this for convenience. In the end $billAddress, $shipAddress and $addressObject all hold the identical object. Objects in PHP are not copied when you assign them to a different variable, the variable only holds a reference to the actual object.

###Correct code:

$billId = 'VALID BILLING ADDRESS ID'; $shipAddress = Mage::getModel('customer/address')->load($shipId); $billAddress = Mage::getModel('customer/address')->load($billId);