1

I have custom module named Rkt_CustomBlock. I am trying to connect it to the database. My table name is socialcoupon. My path of the module is public_html/dev/app/code/local/Rkt/CustomBlock/Model. what I want to know is what should i put in Rkt.php. The xml declaration is working.

     <?php
class Rkt_customblock_Model_Mysql4_Rkt extends Mage_Core_Model_Mysql4_Abstract
{
     public function _construct()
     {
         $this->_init('rkt/socialcoupon', 'scid');
     }
}

I am confused about $this->init. what should i put in it? is it rkt/rkt? or something else

my config file

<?xml version = "1.0"?>
<config>
    <modules>
        <Rkt_CustomBlock>
            <version>1.0.0</version>
        </Rkt_CustomBlock>
    </modules>
    <frontend>
        <routers>
            <rkt_customblock>
                <use>standard</use>
                <args>
                    <module>Rkt_CustomBlock</module>
                    <frontName>helloworld</frontName>
                </args>
            </rkt_customblock>
        </routers>
        <layout>
            <updates>
                <rkt_customblock>
                    <file>rkt_customblock.xml</file>
                </rkt_customblock>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <rkt_customblock>
                <class>Rkt_CustomBlock_Block</class>
            </rkt_customblock>
        </blocks>
        <models>
            <customblock>
                <class>rkt_customblock_Model</class>
                <resourceModel>customblock_mysql4</resourceModel>
            </customblock>
            <customblock_mysql4>
                <class>rkt_customblock_Model_Mysql4</class>
                <entities>
                    <socialcoupon>
                        <table>socialcoupon</table>
                    </socialcoupon>

                </entities>
            </customblock_mysql4>
        </models>
        <resources>
            <customblock_setup>
                <setup>
                    <module>rkt_customblock</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </customblock_setup>
            <customblock_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </customblock_write>
            <customblock_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </customblock_read>
        </resources>
    </global>
</config>
5
  • show your config.xml file please ? Commented Feb 23, 2015 at 1:22
  • thats my config file.
    – beginner
    Commented Feb 23, 2015 at 2:15
  • what did you edit? i am confused
    – beginner
    Commented Feb 23, 2015 at 2:33
  • the codes are not aligned well. I just aligned it correctly :-) Commented Feb 23, 2015 at 2:42
  • Do you know what should be inside my model? I am little confused with it.
    – beginner
    Commented Feb 23, 2015 at 2:48

1 Answer 1

3

In Magento you can call your custom entity socialcoupon in this way.

Mage::getModel('rkt_customblock/socialcoupon')->load(5); //this will load 
      //social coupon with id = 5;

This short code will load a socialcoupon item with an id = 5 from the database. Let us look how magento correctly "parse" your entity here.

General format : Mage::getModel('[model_group_name]/[entity_name]').

In our case model group name is rkt_customblock and entity name is socialcoupon. Using model group name, magento will identify which module responsible for the specified model group name and then locate the model location folder. We are setting up these values via config.xml.

Similarly socilacoupon entity should also set up via config.xml. So your module should hold following model configuration.

File : app/code/local/Rkt/CustomBlock/etc/config.xml

   <models>
        <rkt_customblock> <!-- model group name -->
            <class>Rkt_CustomBlock_Model</class> <!-- model location indicator -->
            <resourceModel>rkt_customblock_resource</resourceModel>
        </rkt_customblock>
        <crkt_customblock_resource>
            <class>Rkt_CustomBlock_Model_Resource</class>
            <entities>
                <socialcoupon> <!-- entity name -->
                    <table>socialcoupon</table> <!-- table name -->
                </socialcoupon>
            </entities>
        </rkt_customblock_resource>
    </models>

Important points note to here are

  1. <rkt_customblock> : this is the model group name. Rule for model group name is, it should be unique. You have used customblock. There is no problem in that. But there is a huge chance to have that model group name is used by any other extension. So in order to avoid such conflicts, magento best practice says that, model group name should follow namespace_modulename format. So in your case it would become rkt_customblock.

  2. <class />: When magento called out by a particular model group name, then magento will look into this node in order to trace the model location for that module group name. Here we set this node with a value Rkt_CustomBlock_Model.Please note, those capitalizations in the value are very important here. This means magento now will look into app/code/local/Rkt/CustomBlock/Model/ directory for all of your module's model classes.

  3. <resourcemodel /> : Resource models are used to communicate with the database. They are making real communication with database. When you call Mage::getModel(), magento will internally call Mage::getResourceModel(). This method uses configurations provided inside this node and hence it is very important to configure this node correctly. Here we are set rkt_customblock_resource to this node. You have used customblock_mysql4. You can use that value too. But it is deprecated. From Magento 1.6, magento follows the practice [model_group_name]_resource. So in our case, it would become rkt_customblock_resource.

  4. <rkt_customblock_resource /> : this node configuring resource model configuration. The name of this node is came from <resourcemodel /> node. So when you call Mage::getModel(rkt_customblock/[entityname]), then magento will internally call the method Mage::getResourceModel('rkt_customblock/[entityname]'), which then eventually ends up in <class /> node which comes inside <rkt_customblock_resource /> node. This <class /> node specifies where is resource model classes are residing. We set a value Rkt_CustomBlock_Model_Resource here. Capitalization is again very important here. According to this value, magento will look for resource model classes inside app/code/local/Rkt/CustomBlock/Model/Resource/ directory.

  5. <entities />: this is used to setup an entity inside our model. Here we setup socialcoupon entity. <table /> node holds table name used by socialcoupon entity.

Now we studied model configuaration we needed. Now we need to setup socialcoupon entity and its resources inside module's mode section. Let us do that.

File Structure

rootdir
|________ app
        |________ code/local/Rkt/CustomBlock
                |________ etc
                |           - config.xml
                |________ Model
                        |   - Socialcoupon.php
                        |________ Resource
                                |   - Socialcoupon.php
                                |_________ Socialcoupon
                                                - Collection.php

First setup a folder structure like this (along with other files). Captitalizations are very important here.

Entity Definition

File : app\code\local\Rkt/CustomBlock/Model/Socialcoupon.php

<?php
class Rkt_CustomBlock_Model_Socialcoupon extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        //follows the format [model_group_name]/[entity_name]
        $this->_init('rkt_customblock/socialcoupon');
    }
}

Calling to _init() via _construct() method is very important. Every model entity should follow this format.

Resource Model Entity Definition

File : app\code\local\Rkt/CustomBlock/Model/Resource/Socialcoupon.php

<?php
class Rkt_CustomBlock_Model_Resource_Socialcoupon extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        //follows the format [model_group_name]/[entity_name]
        $this->_init('rkt_customblock/socialcoupon', 'sc_id');
    }
}

There are two types of models. Flat Models and EAV Models. They are distinguished by reosurce model setup. What is these two types models is out of topic here. Here I am going to assume you want to setup a Flat Model. For flat models, your resourece model class should extend from Mage_Core_Model_Resource_Db_Abstract. This class is also contain _construct() class and it agains calls _init() method within it. But this time, an additional parameter passing. sc_id stands for the primary column of your table. Replace this paramter with your primary column name of your table.

Resource Model Collection Entity Definition

Job is not over. By above your configuration, you can get a single item from your entity by calling

 Mage::getModel('rkt_customblock/socialcoupon)->load(1); //provided some data exist in table.

However if you want to get a collection of items, then you need to configure collection resource model for your entity. This is what we are going to do here.

File : app\code\local\Rkt/CustomBlock/Model/Resource/Socialcoupon/Collection.php

<?php
class Rkt_CustomBlock_Model_Resource_Socialcoupon_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected function _construct()
    {
        //follows the format [model_group_name]/[entity_name]
        $this->_init('rkt_customblock/socialcoupon');
    }
}

Main point note here is, this class extends from Mage_Core_Model_Resource_Db_Collection_Abstract and it also contains _construct() method as all other method does.

Now you are good to go clear your cache and tries to load your model with this call

 Mage::getModel('rkt_customblock/socialcoupon');

Hope that will help

8
  • Did you read my answer !!! ? Commented Feb 23, 2015 at 4:44
  • i was little confused about how to use getModel inside model. So if i want to load data in a form. can i use $hh= Mage::getModel('rkt_customblock/socialcoupon'); and load by id. then echo it?
    – beginner
    Commented Feb 23, 2015 at 4:45
  • There is one thing i dont quiet understand. you didnt use core_read and core_write on config? i dont need it?
    – beginner
    Commented Feb 23, 2015 at 4:47
  • yes of course. you need to print_r() it because it is going to give you an object. It will give some error if you didnt configure your model correclty Commented Feb 23, 2015 at 4:48
  • Oh I fortgot to tell about that code snippet. it is optional as far as I know.If you didnt configure it, then magento will use default read write adapter. However magento best practices says that, it is good to include your own read/write adapter. So my answer for that question is, yes you need to use that code. But there is some error in your configuration. I will update my answer and let you know Commented Feb 23, 2015 at 4:50

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.