1

I managed to put up a EcomDev_PHPUnit_Test_Case with this:

public function testGetModelMemoryTrace() {
    $model = Mage::getModel('catalog/product');
    $product = $model->load(10);
    $this->assertEquals($product->getId(), 10);
    $parentProduct = $model->load(4);
    $this->assertEquals($product->getId(), 10); //here getId() is actually 4...
}

This test fails for me in the last assert. Is this an expected effect ? To clarify, the second load alters first reference to object id 10.

Thank you.

3

$model->load doesnt return a new instance. It returns itself. So $model, $product and $parentProduct is actually the same instance.

| improve this answer | |
  • 1
    exactly, while Mage::getModel('catalog/product') is a factory and returns always a new instance, the ->load() is only a method which returns $this. – Flyingmana Oct 25 '14 at 11:21
-1

Problem is here... When you load prroduct at second time the yoy took variable $parentProduct and you are passing variable $product.

$product contain reference of product id 10 and

$parentProduct contain reference of product id 4.

Thant's why you are getting 10 as product id at second time. So either need to change $product->getId() to $parentProduct->getId() or when you are loading product at second time for id 4 then re-assign it to variable $product.

Hope this will help you.

| improve this answer | |
  • I intentionally did that to show collateral effect of model loadings. Please run the test and you will understand. – Niloct Oct 25 '14 at 8:42
  • I understand, you are correct that load doesn't return an instance but look variable $product have data of product id 10 and once its initialized, then even if you change the instance data by loading another product then still variable $product have data which was loaded for product id 10 because it have own memory space. So wherever you use that variable it will always return 10. – Kumar M Oct 26 '14 at 10:17
  • You missed the point that $model, $product, $parentProduct all point to the same object. So you said that once loading product id 10, that will always remain even with another load ? That is not true, because loading product id 4 after 10 changed these three references. Read the other answer and other comment please. – Niloct Oct 26 '14 at 15:45
  • I never said that they aren't pointing same object. They are pointing same object i know but i am talking about the variables($product and $parentProduct) that you are using to assign every time for different product data. – Kumar M Oct 26 '14 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.