Creating a Magento custom model
In this tutorial, we will create a custom model that uses a different name than the Module name. The Module tree will look like as shown in below:
-Block/
-controllers/
-Model/
-Resource/
-Custommodel/
-Collection.php
-Custommodel.php
-Custommodel.php
-etc/
-config.xml
-sql/
1
2
3
4
5
6
7
8
9
10
11
12
|
–Pixafy/
–Block/
–controllers/
–Model/
–Resource/
–Custommodel/
–Collection.php
–Custommodel.php
–Custommodel.php
–etc/
–config.xml
–sql/
|
In config.xml, we will set up our custom Model configuration. There are three things that we need to setup for a Model in the config.xml. 1. Enable Models in our Module. 2. Enable Model resources in our Module. 3. Add an entity table to our Model Resource.
<pixafy>
<class>[Frontname]_Pixafy_Model</class>
<resourceModel>pixafy_resource</resourceModel>
</pixafy>
</models>
1
2
3
4
5
6
|
<models>
<pixafy>
<class>[Frontname]_Pixafy_Model</class>
<resourceModel>pixafy_resource</resourceModel>
</pixafy>
</models>
|
The <pixafy/> tag is our Group name and should match our module name. The <class/> tag is the Base name or Class prefix, that tells all models in the pixafy group where to look for the actual model objects. [Frontname]_Pixafy_Model is equivalent to Frontname/Pixafy/Model which is the path all our models. The <resourceModel/> tag indicates which Resource Model that Pixafy group Model should use. We will have to create the <pixafy_resource/> configure the resource object that is in Pixafy/Model/Resource/Custommodel.php.
<pixafy>
<class>[Frontname]_Pixafy_Model</class>
<resourceModel>pixafy_resource</resourceModel>
</pixafy>
<pixafy_resource>
<class>[Frontname]_Pixafy_Model_Resource</class>
<entities>
<custommodel>
<table>[table_name_in_database]</table>
</ custommodel >
</entities>
</pixafy_resource>
</models>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<models>
<pixafy>
<class>[Frontname]_Pixafy_Model</class>
<resourceModel>pixafy_resource</resourceModel>
</pixafy>
<pixafy_resource>
<class>[Frontname]_Pixafy_Model_Resource</class>
<entities>
<custommodel>
<table>[table_name_in_database]</table>
</ custommodel >
</entities>
</pixafy_resource>
</models>
|
The resource model <pixafy_recource/> contains the configuration that communicates with our database. Again, we specified where the resource is located in <class/> tag. The <entities/> section tells Customemodel.php in the resource which table in the Database to connect to.
Now we have finished with Config.xml. We then have to create the Model class and Resource class. Pixafy/Model/Custommodel.php will contain the following code:
{
protected function _construct()
{
$this->_init(‘pixafy/custommodel’);
}
}
1
2
3
4
5
6
7
|
class Frontname_Pixafy_Model_Custommodel extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init(‘pixafy/custommodel’);
}
}
|
All basic Models that interact with the database should extend the Mage_Core_Model_Abstract class. This abstract class forces you to implement a single method named _construct (NOTE: this is not PHP’s constructor__construct). This method should call the class’s _init method with the same identifying URI you’ll be using in the Mage::getModel method call.
Pixafy/Model/Resource/Custommodel.php will contain the following code:
protected function _construct()
{
$this->_init(‘pixafy/custommodel’, ‘custommodel_id’);
}
}
1
2
3
4
5
6
|
class Frontname_Pixafy_Model_Resource_Custommodel extends Mage_Core_Model_Resource_Db_Abstract{
protected function _construct()
{
$this->_init(‘pixafy/custommodel’, ‘custommodel_id’);
}
}
|
Again, the first parameter of the init method is the URL used to identify the Model. The second parameter is the database field that uniquely identifies any particular column. In most cases, this should be the primary key.
So, having a single Model is useful, but sometimes we want to grab a list of Models. Rather than returning a simple array of Models, each Magento Model type has a unique collection object associated with it. These objects implement the PHP IteratorAggregate and Countable interfaces, which means they can be passed to the countfunction, and used in for each constructs. Let’s create the Collection class.
Pixafy/Model/Resource/Custommodel/Collection.php should contain the following code:
protected function _construct()
{
$this->_init(‘pixafy/custommodel’);
}
}
1
2
3
4
5
6
|
class Frontname_Pixafy_Model_Resource_Custommodel_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
protected function _construct()
{
$this->_init(‘pixafy/custommodel’);
}
}
|
And that’s it; our model is ready to be used. Let’s test our model by calling model:
1
|
$model = Mage::getModel(‘pixafy/custommodel’);
|
To query a collection, we will call:
1
|
$collections = Mage::getModel(‘pixafy/custommodel’)->getCollection();
|
References: