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:

1 2 3 4 5 6 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> :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 < 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 > < resources > < test_write > < connection > < use >core_write</ use > </ connection > </ test_write >

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

<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:

1 2 3 4 5 6 7 8 9 <?php class Pfay_Test_Model_Test extends Mage_Core_Model_Abstract { public function _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:

1 2 3 4 5 6 7 8 <?php class Pfay_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract { public function _construct() { $this ->_init( 'test/test' , 'id_pfay_test' ); } }

This is where you specify your model, a Magento test/test will use as primary key the id_pfay_test . (Be careful that this field is in auto increment and is the primary key of your table.

Then go to the folder app/code/local/Pfay/Test/Model/Mysql4/Test/ and create a file Collection.php which will contains:

1 2 3 4 5 6 7 8 9 <?php class Pfay_Test_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract { public function _construct() { parent::_construct(); $this ->_init( 'test/test' ); } }

This file is used to define the model for your collection test/test.

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?php class Pfay_Test_Block_Myblock extends Mage_Core_Block_Template { public function methodblock() { $retour = '' ; $collection = Mage::getModel( 'test/test' )->getCollection() ->setOrder( 'id_pfay_test' , 'asc' ); foreach ( $collection as $data ) { $retour .= $data ->getData( 'nom' ). ' ' . $data ->getData( 'prenom' ) . ' ' . $data ->getData( 'telephone' ). '<br />' ; } Mage::getSingleton( 'adminhtml/session' )->addSuccess( 'Cool Ca marche !!' ); return $retour ; } }

Looking at the code, I think you’ll understand what we did with comments, if this is not the case feel free to leave a comment.

Now test your code by going to http://yoursite.com/test/index and you’ll see your contact list.

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

End of this tutorial

Here is the end of this tutorial, I hope this tutorial helps you, you now know to interact with the database in your Magento extensions. Feel free to leave comments if you have any questions or comments