Monday , December 11 2017
Home >> All tutorial >> Magento Developers’ Guides – Part 4: The Model and The Database

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:

  1. CREATE TABLE `magento`.`pfay_test` (
  2. `id_pfay_test` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  3.  `nom` VARCHAR( 50 ) NOT NULL ,
  4.  `prenom` VARCHAR( 50 ) NOT NULL ,
  5.  `telephone` VARCHAR( 20 ) NOT NULL
  6. );

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. <models>
  2. <test>
  3.      <class>Pfay_Test_Model</class>
  4.      <resourceModel>test_mysql4</resourceModel>
  5.  </test>
  6. <test_mysql4>
  7.      <class>Pfay_Test_Model_Mysql4</class>
  8.      <entities>
  9.          <test>
  10.            <table>pfay_test</table>
  11.          </test>
  12.       </entities>
  13. </test_mysql4>
  14. </models>
  15.     <!– allow the plugin to read and write –>
  16. <resources>
  17.         <!– connection to write –>
  18.         <test_write>
  19.             <connection>
  20.                 <use>core_write</use>
  21.             </connection>
  22.         </test_write>
  23.         <!– connection to read ->
  24.        <test_read>
  25.           <connection>
  26.              <use>core_read</use>
  27.           </connection>
  28.        </test_read>
  29. </resources>
  30.  <!– -/- –>

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:

  1. <?php
  2. class Pfay_Test_Model_Test extends Mage_Core_Model_Abstract
  3. {
  4.      public function _construct()
  5.      {
  6.          parent::_construct();
  7.          $this->_init(‘test/test’);
  8.      }
  9. }

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. <?php
  2. class Pfay_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
  3. {
  4.      public function _construct()
  5.      {
  6.          $this->_init(‘test/test’, ‘id_pfay_test’);
  7.      }
  8. }

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. <?php
  2. class Pfay_Test_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
  3.  {
  4.      public function _construct()
  5.      {
  6.          parent::_construct();
  7.          $this->_init(‘test/test’);
  8.      }
  9. }

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. <?php
  2. class Pfay_Test_Block_Myblock extends Mage_Core_Block_Template
  3. {
  4.      public function methodblock()
  5.      {
  6.         //on initialize la variable
  7.         $retour=;
  8.         /* 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 */
  9.      $collection = Mage::getModel(‘test/test’)->getCollection()
  10.                                  ->setOrder(‘id_pfay_test’,‘asc’);
  11.          /* then, we check the result of the query and with the function getData() */
  12.         foreach($collection as $data)
  13.         {
  14.              $retour .= $data->getData(‘nom’).‘ ‘.$data->getData(‘prenom’)
  15.                      .‘ ‘.$data->getData(‘telephone’).‘<br />’;
  16.          }
  17.          //i return a success message to the user thanks to the Session.
  18.          Mage::getSingleton(‘adminhtml/session’)->addSuccess(‘Cool Ca marche !!’);
  19.          return $retour;
  20.       }
  21.  }

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 ;)

Tutorial by: pierrefay.com

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics