Magento Model Basics Part 1 – Creating Your Model

Creating and manipulating models is a fundamental part of Magento development, so I thought I’d do a mini-series on the basics of setting up a new custom model for your Magento store. In this example I’m going to create a photograph model because maybe I want to upload and store photographs in my Magento admin because I’m a keen amateur photographer.
Namespace: Creare
Module: Photo
These are the initial steps that you need to follow when creating a model, and this is what I’ll cover in this post:
- Create Model constructor class
- Create Resource Model class
- Create Model Collection class
- Create Database table for model
etc/config.xml
To start with, within your <config> node we need to declare the module version so that later we can create install/upgrade scripts.
<modules>
<Creare_Photo>
<version>0.1.0</version>
</Creare_Photo>
</modules>
Next we declare our model name within the node, which in this case be ‘photo’. Inside this we put our model class name which is simply the directory path to your model folder. We also specify our resource model name. After this we use our newly defined resource model name inside a self-titled node where we declare its class (again the file path), and database table name (which we’ll look at later).
<models>
<photo>
<class>Creare_Photo_Model</class>
<resourceModel>photo_mysql4</resourceModel>
</photo>
<photo_mysql4>
<class>Creare_Photo_Model_Mysql4</class>
<entities>
<photograph>
<table>photo_photograph</table>
</photograph>
</entities>
</photo_mysql4>
</models>
The final bit of configuration we need to do is again in our <global> node. When working with models, everything is pretty much scoped globally. We just need to make Magento aware if our install/upgrade scripts.
<resources>
<photo_setup>
<setup>
<module>Creare_Photo</module>
<class>Creare_Photo_Model_Mysql4_Setup</class>
</setup>
</photo_setup>
</resources>
Using the resources node, you can then call the inside node anything you like – just remember that you’ll need to use this name in your setup path later on (inside the sql directory).
Model/Photograph.php
This model initialisation class is as simple as it looks. From this point onwards we have a model type called ‘photo/photograph’. Always extends the Mage_Core_Model_Abstract class.
<?php
class Creare_Photo_Model_Photograph extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('photo/photograph');
}
}
Model/Resource/Photograph.php
We have now initialised our resource model, allowing our model to interact with the database. We simply pass in the model name and the database table primary key (which we haven’t created yet).
<?php
class Creare_Photo_Model_Resource_Photograph extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('photo/photograph', 'entity_id');
}
}
Model/Resource/Photograph/Collection.php
We can now use our resource model to create the ability to grab a collection of our models, which we can do by simply initialising our collection class.
<?php
class Creare_Photo_Model_Resource_Photograph_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
protected function _construct() {
$this->_init('photo/photograph');
}
}
Almsot there! We now have our model setup and configuration pretty much done, but with one problem. Where’s the data? Well, remember the photo_setup config that we set earlier? We’re going to give it what it’s asking for by creating our install file, which will create the database table ‘creare_photo’.
Model/Mysql4/Setup.php
We start by creating a setup class so we can create an instance of the install object. We don’t need to change anything in the original core setup class so you can leave it blank.
<?php
class Creare_Photo_Model_Mysql4_Setup extends Mage_Core_Model_Resource_Setup
{
}
sql/photo_setup/mysql4-install-0.1.0.php
Remember it’s good Magento practice to alias $this object to $installer. I’m gonna create a pretty basic table to demonstrate how it works.
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE `{$installer->getTable('photo/photograph')}` (
`entity_id` int(11) NOT NULL auto_increment,
`title` text,
`img_path` text,
`date` datetime default NULL,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`entity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
Load a page in your admin or frontend to run the install script. Check your database for the table to ensure that it has worked. If not check the ‘core_resource’ table for your ‘photo_setup’ record. If it’s not there, check your versions match. Your install script needs to be equal or higher than your Magento module version to run.
We now have a model, but no data to make instances of this model. In part 2, I’ll be going through how to make this model editable in your Magento admin so that you can add new data to the collection. You’ll then be able to actually get some data when you instantiate your model in the following ways:
Mage::getModel('photo/photograph');
Mage::getResourceModel('photo/photograph_collection');
Thanks for reading.
Adam is Ecommerce Manager and a PHP developer at Creare Group. Adam is responsible for training Magento development within the company. Follow Adam on Twitter: http://twitter.com/adampmoss. - Read my other posts.

When is the next part due? This is very helpful.
Thanks John, part two will be in the next two weeks.