1

This is my model Class

<?php

class Magentotutorial_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
protected function _construct()
{
    $this->_init('weblog/blogpost');
}
}
?>

My question is What does this code do in magento?

$this->_init('weblog/blogpost');

1 Answer 1

4

It initiates the resource model this model uses to commit data to the database.

So in this case that would be the file app/code/[local|community]/Magentotutorials/Weblog/Model/Resource/Blogpost.php

[EDIT] to answer @easymoden00b question, the _init method can be found in Mage_Core_Model_Abstract which directly calls _setResourceModel in the same class

/**
 * Set resource names
 *
 * If collection name is ommited, resource name will be used with _collection appended
 *
 * @param string $resourceName
 * @param string|null $resourceCollectionName
 */
protected function _setResourceModel($resourceName, $resourceCollectionName=null)
{
    $this->_resourceName = $resourceName;
    if (is_null($resourceCollectionName)) {
        $resourceCollectionName = $resourceName.'_collection';
    }
    $this->_resourceCollectionName = $resourceCollectionName;
}

This stores the Resource item and collection class names for further use when retrieving and writting to and from the database

2
  • 1
    related: what does the _init() function do? Commented Dec 17, 2014 at 13:47
  • @easymoden00b made an edit :) Commented Dec 17, 2014 at 14:09

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.