Monday , December 11 2017
Home >> All tutorial >> Magento Developers’ Guide- Part 5: The model, collections and forms

Magento Developers’ Guide- Part 5: The model, collections and forms

We have in the previous tutorials creates a module with a controller, a block and its template and a connection to the database. We will now see how to go further with the database and how to better interact with magento.

As we saw earlier to retrieve data from your database, you have to use the getModel , which takes as parameters the type of object, in the previous tutorial we defined an object type test/test in our model.

So we will get this model by:

  1. Mage::getModel(‘test/test’);
With this model you can store messages and nicknames and you can retrieve an object in your database. If I want to get for example the « test » object with the identifier 2, you will do:
  1. Mage::getModel(‘test/test’)->load(‘2’);
Now we want to retrieve all objects of this type that have been registered in our database. To do this we will use what is called a collectionwe will get through the method getCollection from our model.
  1. $macollection =  Mage::getModel(‘test/test’)->getCollection();
With this collection, we can make operation on our database such as:
  1. $macollection =  Mage::getModel(‘test/test’)->getCollection();
  2. //sorted by ID
  3. $macollection->setOrder(‘id_pfay_test’,‘asc’);
  4. //only select those withe the phone number 03 20 58 74 89
  5. $macollection->addFilter(‘telephone’, ‘0320587489’);

Now, what would you say if you put your module in a form to add phone numbers directly from the frontend and it sorts the entries by user name when viewing? anyone? Ok here we go!

1 – Sort data when viewing

We have seen in the previous tutorial, your contact list is displayed in your block

So open Monbloc.php located in app/code/local/Pfay/Test/Block and change this line:

  1. $collection = Mage::getModel(‘test/test’)
  2.                     ->getCollection()
  3.                     ->setOrder(‘id_pfay_test’,‘asc’);

You notice it’s done in this line of code (it’s crazy it’s really good ^ ^).

2 – Add the Form

Edit the Template afficher.phtml located in \app\design\frontend\pfay\theme\template\test\ as this:

  1. <form action=”<?php echo Mage::getUrl(‘test/index/save’) ?>” method=”post”>
  2.   <fieldset>
  3.     <ul>
  4.      <li>
  5.        <label for=“nom”>Nom</label>
  6.        <input type=“text” id=“nom” name=“nom” />
  7.      </li>
  8.      <li>
  9.          <label for=“prenom”>Prenom</label>
  10.          <input type=“text” id=“prenom” name=“prenom” />
  11.      </li>
  12.      <li>
  13.         <label for=“nom”>Telephone</label>
  14.         <input type=“text” id=“telephone” name=“telephone” />
  15.     </li>
  16.      <li>
  17.         <input type=“submit” value=“Save” />
  18.     </li>
  19.   </ul>
  20.  </fieldset>
  21. </form>
  22. <?php
  23.    echo $this->methodblock();
  24.  ?>

Explanation: This is a form, it send the informations to the method save of your controller IndexController of your module Test . The url will be found using the method getUrl.

Now, what happen when we send the form? Open the controller IndexController of your module Test (/app/code/local/Pfay/test/controllers /) and add the following method:

  1. public function saveAction()
  2.  {
  3.     //on recuperes les données envoyées en POST
  4.     $nom = .$this->getRequest()->getPost(‘nom’);
  5.     $prenom = .$this->getRequest()->getPost(‘prenom’);
  6.     $telephone = .$this->getRequest()->getPost(‘telephone’);
  7.     //on verifie que les champs ne sont pas vide
  8.     if(isset($nom)>>($nom!=) >> isset($prenom)>>($prenom!=)
  9.                                >> isset($telephone)>>($telephone!=) )
  10.    {
  11.       //on cree notre objet et on l’enregistre en base
  12.       $contact = Mage::getModel(‘test/test’);
  13.       $contact->setData(‘nom’, $nom);
  14.       $contact->setData(‘prenom’, $prenom);
  15.       $contact->setData(‘telephone’, $telephone);
  16.       $contact->save();
  17.    }
  18.    //on redirige l’utilisateur vers la méthode index du controller indexController
  19.    //de notre module <strong>test</strong>
  20.    $this->_redirect(‘test/index/index’);
  21. }

To understand what i’ve made, read the comments in the code, it retrieves information on the form and saves them on base and then redirects to the index method.

Note that this piece of code that I put in the save method, we could have put it in the method of the block in order to record information in your list no matter where on your website you insert the block … it works well.


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