Model, collections and forms

In the previous tutorials, we created a module with a controller, a block, a 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.

The model, collections, forms ...

As we have seen, to retrieve data from your database, you can use the getModel function, which takes the object type in parameter. In the previous tutorial we defined an object type pfay_films/film for our model "Film" which is actually the table "pfay_films" in our database and it represent the movies collection you have in a database.


Thanks to this model you can get a Film object stored in your database. If I want to recover my example Film with the id "2", I will do:


Now we want to get all the Film that have been recorded in our database. For this we will use a collection. You can get a collection with the function getCollection of your Model.

      $macollection =  Mage::getModel('pfay_films/film')->getCollection();

With this collection, we can make a lot of operations on our database such as:

     $collection =  Mage::getModel('pfay_films/film')->getCollection();

     //order by ID ASC

     //filter by name "Gladiator"
     $macollection->addFilter('name', 'Gladiator');

Now, we will we put a form to add new movies and that sorts the entries in the list alphabetically. OK here we go !

1- Sorting data when viewing the list

We saw in the previous tutorial, your contact list is displayed in your block. Open the file Monbloc.php located in app/code/local/Pfay/Films/Block and change this line:

    $collection = Mage::getModel('pfay_films/film')

There it's finished ! (It's crazy, this tutorial is really well done ^^ ).

2- Add a new Form

Edit the template afficher.phtml in app\design\frontend\pfay\theme\template\pfay_films\ like this :

<form action="<?php echo Mage::getUrl('films/index/save') ?>" method="post">
       <label for="nom">Name</label>
       <input type="text" id="name" name="name" />
        <input type="submit" value="Enregistrer" />

<?php echo $this->methodblock(); ?>

This is a form, you send it to the method save of your magento controller IndexController of your module Films.
The url will be fuond thanks to the getUrl() method. Now, what will you happen when we will send the form? Open the controller IndexController of your Films module (/app/code/local/Pfay/Films/controllers/) et add the following method :

 public function saveAction()
    //we get the datas sent with POST
    $name = ''.$this->getRequest()->getPost('name');

    //we check that the name is not empty
    if( isset($name) && ($name!='') )
      //we create an entry in the database
      $contact = Mage::getModel('pfay_films/film');
      $contact->setData('name','Demain ne meurt jamais');

     //we create an entry in the database
     // but this time we will use setName ( the method exist automatically in magento for every attribute with their name setXXX where XXX is the name of your attribute).
      $contact = Mage::getModel('pfay_films/film');
      $contact->setName('Gladiator 3');

   //we redirect the user to the index method of the indexController
   // of our module films

understand what I am doing above, read the comments in the code, we get the form information and record it in the database. Then redirects to the index method of our controller.

Homework :
Repeat this tutorial, try to add new informations in your model and your form...practice, this is how we get there. If you have a question or a constructive remark you can leave a comment at the bottom of this article. If you are stucked feel free to download the code to compare with yours. Congratulations you've managed to keep until the end of this tutorial, in the next we will create the backend interface.
Available documents for this article :
Books that can help you :
  • Livre Magento Developer's Guide by Alan Mc Gregor
  • Livre Magento Performance Optimization
  • Livre Grokking Magento Vinai
Questions about this lesson
No questions for this lesson. Be the first !

You must be logged in to ask for help on a lesson.