4

I'm having a curious problem in model's _construct function.

When the model is instantiated I would like to call a method to pull and save an access key from a database into a class property so it is immediately available, so I'm calling the method from within the _construct():

private $active_key

protected function _construct()
{
    $this->_init('foo/foo');
    $this->active_key = $this->getActiveKey();
}

The Method

public function getActiveKey()
{
    return $this->getCollection()
        ->addFieldToSelect('foo_key')
        ->addFieldToFilter(
            'foo_timestamp',
            array('gt'=>$this->threshold)
        )
        ->getFirstItem()
        ->getFooKey();
}

If I call getActiveKey() from a controller, all's fine and it returns the correct data. Using $this->getActiveKey() in the _construct, however, prompts a "No Data Received" error from our servers when instantiating the model class. Is there some sort of restriction against methods in the model _construct that I'm unaware of?

I know I could just chain the methods I require in the controller that will use the model, but I would like to understand why this is not working like I think it should.

4
  • does you model extend Mage_Core_Model_Abstract or something else?
    – Marius
    Feb 23, 2015 at 16:05
  • Yes. Extends Mage_Core_Model_Abstract. Feb 23, 2015 at 16:14
  • I assume the > isn't in the init call in your actual code? Feb 23, 2015 at 16:55
  • Nope. Typing error. Feb 23, 2015 at 19:49

1 Answer 1

4

The _construct function is triggered from Varien_Object::__construct, this means it gets triggered every time you instantiate a new instance of your model. Therefore you are calling code from this constructor that will get the first element of a collection of the same object type. This means instantiating a new instance of the model. Which in turn triggers the constructor, which in turn loads the collection to get the first item, which in turn calls the constructor, which in turn loads the collection, which in turn gets the first item....

I'm sure you can see where this is going :)

2
  • So I'm inadvertently creating a infinite loop? That makes sense. I suppose its a little confusing because of Magento's naming conventions. Thanks for clarifying. Feb 23, 2015 at 19:45
  • 1
    Yup, it took me awhile to realise, because of the whole method chaining and hidden logic going on behind the scenes it's really easy to miss the fact you're creating another instance of the same class from the constructor, which is always going to cause an infinite loop without some kind of static protection against it. The 'fix' would probably be to add a helper method to the resource model that performs a simple sql query to fetch the information without using models. Feb 23, 2015 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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