Lesson 2 - The model and the database

26 Aug 2014
In the last lesson, I have introduced you how to create a module according to the simplest and easiest steps. If you have not read the previous post yet, please read again from the beginning to understand this lesson easily. In this second post, I will introduce you the way to create a Model and to use database for a module.

Dear fellows, in this lesson, I am happy to have opportunity to share experience about creating Magento extensions with you.

In the last lesson, I have introduced you how to create a module according to the simplest and easiest steps. If you have not read the previous post yet, please read again from the beginning to understand this lesson easily.

In this second post, I will introduce you the way to create a Model and to use database for a module.

CREATE THE TABLE

In the first post, we have learnt to develop a module: SimpleNews. This module, basically, has features as follows:

+ Manage news easily

+ Display news in frontend

Hence, we have 2 tables:

+ simplenews_category (id, category_name, category_desc, status): save information about news category.

+ simplenews_news (id, title, category_id, desc, status): save information about news

* TABLE simplenews_news

CREATE TABLE `simplenews_news` (
`id`  int(10) NOT NULL AUTO_INCREMENT ,
`title`  varchar(255) NOT NULL ,
`category_id`  int(10) NOT NULL ,
`desc`  varchar(255) NOT NULL ,
`status`  varchar(30) NOT NULL DEFAULT disabled ,
PRIMARY KEY (`id`)
);

Your table is now in the database. To interact with it, we’ll create the model.

DECLARE THE MODEL IN CONFIG.XML

As you may know, Magento use MVC structure, this part is very necessary and important for you to create a model that can interact with database.

In this model declaration, you can use a template, then copy &paste, change model and table names.

You can view the full in file config.xml here:

<models>
    <simplenews_model>
        <class>MW_SimpleNews_Model</class>
        <resourceModel>simplenews_model_mysql4</resourceModel>
    </simplenews_model>
    <simplenews_model_mysql4>
        <class>MW_SimpleNews_Model_Mysql4</class>
        <entities>
            <news>
                <table>simplenews_news</table>
            </news>
        </entities>
    </simplenews_model_mysql4>
</models>
<resources>
    <simplenews_model_write>
        <connection>
            <use>core_write</use>
        </connection>
    </simplenews_model_write>
    <simplenews_model_read>
        <connection>
            <use>core_read</use>
        </connection>
    </simplenews_model_read>
</resources>

Explanation:

- In the above declaration, you can see 2 main TAGs:

-  <models>: you must pay attention to 2 parts <resourceModel>  and <simplenews_model_mysql4>, which will be absolutely related. The rest, basically, is whole model of Module, which will be in folder app/code/local/MW/SimpleNews/Model

- <resources>: >: this is used to declare and use database connection for module, you just need copy&paste them for your other modules.

Notes:

One note for you as declaring model, resources in file config:

<simplenews_model_mysql4>
    <class>MW_SimpleNews_Model_Mysql4</class>
    <entities>
        <news>
            <table>simplenews_news</table>
        </news>
    </entities>
</simplenews_model_mysql4>

In tag <entities> >, there are definitions about tables in your module, or in other words, instead of remembering long table’s name, you just rewrite them. Here I define <news> for the table simplenews_news (will see I mention this table in next part).

CREATE THE MODEL

After you have declared the model, resources in file config.xml, next, you need to create necessary files for module. We have 3 files:

- app/code/local/MW/SimpleNews/Model/News.php

- app/code/local/MW/SimpleNews/Model/Mysql4/News.php

- app/code/local/MW/SimpleNews/Model/Mysql4/News/Collection.php

Explaintion:

You should pay attention to this part, 3 files that I created are corresponded with the table that I declare in CREATE THE TABLE part. Hence, with each table that you want to query and collect data, you need to create 3 files as above.

In file app/code/local/MW/SimpleNews/Model/News.php, we have following code: 

class MW_SimpleNews_Model_News extends Mage_Core_Model_Abstract {
    public function _construct() {
        parent::_construct();
        $this->_init('simplenews_model/news');
    }
}

Similarly, in file app/code/local/MW/SimpleNews/Model/Mysql4/News.php, we have following code: 

class MW_SimpleNews_Model_Mysql4_News extends Mage_Core_Model_Mysql4_Abstract{
    public function _construct(){
        $this->_init('simplenews_model/news', 'id');
    }
}

Notes:

$this->_init('simplenews/news', 'id'); id here is id field of table simplenews_news, so if you have different id field for your table, you can rename.

Finally, create file app/code/local/MW/SimpleNews/Model/Mysql4/News/Collection.php, the purpose of this file is for collection data – filter - search data of table.

class MW_SimpleNews_Model_Mysql4_News_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract{
    public function _construct(){
        parent::_construct();
        $this->_init('simplenews_model/news');
    }
}

In short, I have introduced you how to declare model, collect data of table. If you cannot learn by heart, you just need to copy & paste them for next usage.

HOW TO CALL THE  MODEL

In this part, I continue introducing you how to call a model that you have defined in config.xml.

If you still remember in Lesson 1, we are having IndexController.php in folder app/code/local/MW/SimpleNews/controllers. Now we will test the way to call model in this controller.

public function indexAction(){
    $collection = Mage::getModel('simplenews_model/news')->getCollection();
    Zend_Debug::dump($collection->getData());
}

Explaintion:

- Mage::getModel('simplenews_model/news'):  This is the way to call a model, about news, it is file Model/news.php. simplenews_model where does it come from? Yes, it was defined in file config.xml:

<models>
    <simplenews_model>
        <class>MW_SimpleNews_Model</class>
        <resourceModel>simplenews_model_mysql4</resourceModel>
    </simplenews_model>
    …
</models>

OK, now you run this controller and check the result:

Related Posts