The Model, collections and forms

This tutorial is the 5th of many , if you do not have already read them from the start, i suggest you to start with the summary of this series.



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.

model, collections, forms …

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 collection we 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 2 3 4 5 $macollection = Mage::getModel( 'test/test' )->getCollection(); $macollection ->setOrder( 'id_pfay_test' , 'asc' ); $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 2 3 $collection = Mage::getModel( 'test/test' ) ->getCollection() ->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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <form action= "<?php echo Mage::getUrl('test/index/save') ?>" method= "post" > <fieldset> <ul> <li> <label for = "nom" >Nom</label> <input type= "text" id= "nom" name= "nom" /> </li> <li> <label for = "prenom" >Prenom</label> <input type= "text" id= "prenom" name= "prenom" /> </li> <li> <label for = "nom" >Telephone</label> <input type= "text" id= "telephone" name= "telephone" /> </li> <li> <input type= "submit" value= "Save" /> </li> </ul> </fieldset> </form> <?php echo $this ->methodblock(); ?>

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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public function saveAction() { $nom = '' . $this ->getRequest()->getPost( 'nom' ); $prenom = '' . $this ->getRequest()->getPost( 'prenom' ); $telephone = '' . $this ->getRequest()->getPost( 'telephone' ); if (isset( $nom )&&( $nom != '' ) && isset( $prenom )&&( $prenom != '' ) && isset( $telephone )&&( $telephone != '' ) ) { $contact = Mage::getModel( 'test/test' ); $contact ->setData( 'nom' , $nom ); $contact ->setData( 'prenom' , $prenom ); $contact ->setData( 'telephone' , $telephone ); $contact ->save(); } $this ->_redirect( 'test/index/index' ); }

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.

Your homework: again this tutorial, try saving your form data, practice blocks. .., that’s how we get there.

end of this tutorial Congratulations you have reached the end of this tutorial. Leave a message comment, It’s always nice and i love to talk with you guys