Magento Developers' Guides – Part 4: The Model and The Database

We have previously created a plugin with a controller, a block and its associated template. Our plugin works well and I think now you understand how it works. I take the opportunity to thank you for your very positive feedback that motivate me to continue this series of tutorials.

Now, a plugin that is the equivalent of a echo is not very useful and it has nothing very complicated as you have seen. In this tutorial we will modify our plugin to look for data in our database (in MySQL) and display it in our Block.

Step 1: Create the table

We will say that our plugin is an address book, we want the block to display all the book addresses one after the other of the form:

Name Surname telephone_number

Example:

John Smith 0000000000

John Smith 0000000000

John Smith 0000000000

John Smith 0000000000

…

We will start by creating a table in the database, we will call this table pfay_test

So execute the following query:

CREATE TABLE `magento`.`pfay_test`(

`id_pfay_test` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`nom` VARCHAR(50) NOT NULL ,

`prenom` VARCHAR(50) NOT NULL ,

`telephone` VARCHAR(20) NOT NULL

);

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

Step 2: Declare the model in config.xml

You know, Magento use the MVC model, it is necessary to create a model in order to interact with our database.

To create a template, you who have followed the previous three tutorials …what are we going to have to do? You have the answer comes out automatically from your head as you start to get used to ^ ^, we will first declare the model in the config.xml file of our plugin.

Go therefore etc/config.xml and add in <global> after the declaration <blocks> :

<models>

<test>

<class>Pfay_Test_Model</class>

<resourceModel>test_mysql4</resourceModel>

</test>

<test_mysql4>

<class>Pfay_Test_Model_Mysql4</class>

<entities>

<test>

<table>pfay_test</table>

</test>

</entities>

</test_mysql4>

</models>

<!– allow the plugin to read and write –>

<resources>

<!– connection to write –>

<test_write>

<connection>

<use>core_write</use>

</connection>

</test_write>

<!– connection to read ->

<test_read>

<connection>

<use>core_read</use>

</connection>

</test_read>

</resources>

<!–-/-–>

In :

It says here that the model classes wills be in the directory app/code/local/Pfay/Test/Model/ and the « resource » that is to say the thing used to collect data for the model will be defined by test_mysql4

defined in the classes will be in app/code/local/Pfay/Test/Model/Mysql4/ and an entity test represent the table pfay_test we have created earlier in the database.

Step 3: Create the Model

Once your Model declared, you can now create the files. So create the following three files:

app/code/local/Pfay/Test/Model/

app/code/local/Pfay/Test/Model/Mysql4/

app/code/local/Pfay/Test/Model/Mysql4/Test/

Then, in app/code/local/Pfay/Test/Model/ create a file Test.php which will contain the following code:

<?php

classPfay_Test_Model_TestextendsMage_Core_Model_Abstract

{

publicfunction _construct()

{

parent::_construct();

$this->_init(‘test/test’);

}

}

This is your test model, you are telling that there’s a logical entity test of your plugin test .
Then go to the folder app/code/local/Pfay/Test/Model/Mysql4/ and create a file Test.php which will contain:

You ‘re maybe not very well understanding what all this is, it is imperative that you to memorize these steps to create a template in Magento. Once these files are created, you can use your model to interact with your tables very easily.

Step 4: List contacts in our Block

Remember, in the preceding tutorial we have created a block in app/code/local/Pfay/Test/Block called Myblock.php . We will change that block to display the contact list in the database.

First, insert few lines in your database table pfay_test .

Then change your Myblock.php as follows:

<?php

classPfay_Test_Block_MyblockextendsMage_Core_Block_Template

{

publicfunction methodblock()

{

//on initialize la variable

$retour=”;

/* we are doing the query to select all elements of the pfay_test table (thanks to our model test/test and we sort them by id_pfay_test */

$collection =Mage::getModel(‘test/test’)->getCollection()

->setOrder(‘id_pfay_test’,‘asc’);

/* then, we check the result of the query and with the function getData() */

Your homeworks:

Try to repeat this and make a plugin from scratch before starting the next tutorial, you need to know to how to do the 4 firsts tutorials « fingers in the nose » before starting the next tutorial

Practice for exemple in making a new plugin to manage your movie library. It strongly resembles to what we’ve done, but it’s different so you’ll be able to practice and really understand what you are doing. Good luck