In this post, we will see how we can create a simple model in Magento. It is not difficult but require some precise steps, so I would suggest to follow carefully.

If you don’t know about models in Magento, please check this post Understanding models in Magento

Creating a simple model

  1. Make sure you have created a module. If not, click here to see the steps.
  2. Create a new folder named Model inside the Basic folder. Then create a new file named Location.php:
    app\code\local\Test\Basic\Model\Location.php

    Add following code in the file:

    <?php
    class Test_Basic_Model_Location extends Mage_Core_Model_Abstract{   
        protected function _construct()
        {
           $this->_init('basic/location'); 
        }
    }
  3. Next to create a resource model for our simple model, create another folder named Resource under Model and create another file named Location.php:
    app\code\local\Test\Basic\Model\Resource\Location.php

    Add following code in this file:

    <?php
    class Test_Basic_Model_Resource_Location extends Mage_Core_Model_Resource_Db_Abstract{
    
        protected function _construct()
        {
           $this->_init('basic/location', 'id');
        }
    }
  4. Now we will create one more class for collection so that we can read a list of models from databsae. Create a new folder named Location under the Resource folder and create a new file named Collection.php:
    app\code\local\Test\Basic\Model\Resource\Location\Collection.php
    

    Add following code in this file:

    <?php
    class Test_Basic_Model_Resource_Location_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{
        protected function _construct()
        {
            $this->_init('basic/location');
        }
    }
  5. Next, we will tell Magento where to look for our models. For this, make sure your etc\config.xml looks like this:
    <?xml version="1.0" ?>
    <config>
        <modules>
            <Test_Basic>
                <version>1.0</version>
            </Test_Basic>
        </modules>
        <global>
            <models>
                <basic>
                    <class>Test_Basic_Model</class>
                    <resourceModel>basic_resource</resourceModel>
                </basic>
                <basic_resource>
                    <class>Test_Basic_Model_Resource</class>
                    <entities>
                        <location>
                            <table>locations</table>
                        </location>
                    </entities>
                </basic_resource>
            </models>
        </global>
    </config>
  6. Next create the following table in database:
    CREATE TABLE locations (
       id INT(11) NOT NULL AUTO_INCREMENT,
       country VARCHAR(255) NOT NULL,
       state VARCHAR(255) NOT NULL,
       city VARCHAR(255) NOT NULL,
       pin VARCHAR(10) NOT NULL,
       PRIMARY KEY (id));
  7. Our model is ready at this moment. Make sure you refresh your cache. Now to test our model, we will create a file named test.php in the root folder of the website. Add following code in it:
    <?php
    require_once('app/Mage.php');
    
    Mage::app();
    
    $location = Mage::getModel('basic/location');
    
    $location->setCountry('India');
    $location->setState('Uttarakhand');
    $location->setCity('Roorkee');
    $location->setPin('247667');
    
    $location->save();
    
    echo 'Record saved';
  8. Now you can access this file with the URL:
    http://localhost/magento/test.php

    and you will see the message Record saved. Go and check your database table. Change values and run the page again to add some more entries in the table.

  9. Now, we will list all the locations from the table. Create a new file list_locations.php in the root folder of the website and add following code in it:
    <?php
    require_once('app/Mage.php');
    
    Mage::app();
    
    $locations = Mage::getModel('basic/location')->getCollection();
    
    foreach($locations as $location){
        echo 'Country = ' . $location->getCountry() . '<br/>';
        echo 'State = ' . $location->getState() . '<br/>';
        echo 'City = ' . $location->getCity() . '<br/>';
        echo 'Pin = ' . $location->getPin() . '<br/>';
        echo '<hr/>';
    }
  10. Now you can access this file with the URL:
    http://localhost/magento/list_locations.php

    and you should see a list of all the entries in the table.