Creating a Magento custom model

Creating a Magento custom model

Posted by Yongji Chen

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:

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.

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.

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:

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:

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:

And that’s it; our model is ready to be used. Let’s test our model by calling model:

To query a collection, we will call:

References:

Advertisements

Laisser un commentaire

Entrer les renseignements ci-dessous ou cliquer sur une icône pour ouvrir une session :

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google+

Vous commentez à l’aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s