We previously created a module with a controller, a block and a template. Our module works well and I think you understand how it works so far.
I want to thank you for your very positive feedback that motivate me to continue this series of tutorials.
Now, a module that is the equivalent of an "echo" is not very helpful in real life and it's nothing very complicated you saw.
In this tutorial we will modify our module to use information in our database (in MySQL) and display it in our Block template.
Step 1: Create the table
So we will create a table "film" (wich will be ouf db table for all our movies), so we want the block to display all the movie titles available like this:
Name of the movie
Example:
- Gladiator
- Sexy Stupid Love
- Gang of New York
- The Wolf of Wall Street
...
So we need to create a table in the database, we will call this pfay_films table.
we will see later how to create this table automatically when magento detects your Magento module for the first time ( in the lesson "Commit, Rollback & Create a table in Magento" ) but for now we will do it simply.
Create the table through SQL:
CREATE TABLE `magento`.`pfay_films` (
`id_pfay_films` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
);
Your table is now in the database. To make interractions between this table and magento, we will create the object named
Model.
Step 2: Declare the Model in config.xml
You know, Magento uses the MVC model, we must create a model for interracting with our database.
To create a Model, you who have followed the three previous tutorials ... what do we have to ? You answer that automatically comes out of your head as you start to get used ^^ : we will first
declare the model in config.xml of our magento module.
Go therefore etc/config.xml and add in <global>:
<models>
<pfay_films>
<class>Pfay_Films_Model</class>
<resourceModel>pfay_films_resource</resourceModel>
</pfay_films>
<pfay_films_resource>
<class>Pfay_Films_Model_Resource</class>
<entities>
<film>
<table>pfay_films</table>
</film>
</entities>
</pfay_films_resource>
</models>
<resources>
<!-- connection pour ecrire -->
<pfay_films_write>
<connection>
<use>core_write</use>
</connection>
</pfay_films_write>
<!-- connection pour lire-->
<pfay_films_read>
<connection>
<use>core_read</use>
</connection>
</pfay_films_read>
</resources>
In <pfay_films> :
It says here that the Model classes of our module will be in the app/code/local/Pfay/Films/Model/and the "resource" that is to say, the thing used to fetch the data from the model will be defined by pfay_films_resources
In <pfay_films_mysql4>:
magento declare that the classes will be in app/code/local/Pfay/Films/Model/Resource/ and an entity "pfay_Films" represent pfay_films table we created in the beginning of this tutorial.
Step 3: Create the class Model
Once your Model is declared in the config.xml, you can now create the files.
So create the three following folders :
- App/code/local/Pfay/Films/Model/
- App/code/local/Pfay/Films/Model/MySQL4/
- App/code/local/Pfay/Films/Model/MySQL4/Films/
Then, in app/code/local/Pfay/Films/Model/Film.php, create the file containing the following code:
class Pfay_Films_Model_Film extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('pfay_films/film');
}
}
This is your model Film (which represent a movie), you tell magento it's a film entity of your Pfay_Films module.
Then go to the app/code/local/Pfay/Films/Model/Resource/Film.php and create a file that will contain:
class Pfay_Films_Model_Resource_Film extends Mage_Core_Model_Resource_Db_Abstract
{
public function _construct()
{
$this->_init('pfay_films/film', 'id_pfay_films');
}
}
This is where you tell your Magento model pfay_films/film will be used as the primary key field id_pfay_films. (Be careful that this field is in auto-increment and is the primary key of your table).
Then go to the app/code/local/Pfay/Films/Model/Resource/Film/Collection.php and create a file that will contain:
class Pfay_Films_Model_Resource_Film_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('pfay_films/film');
}
}
This file is used to define the collection for your model pfay_films/film
You do not have to understand everything but it is essential for you to remember this for
create a Magento model. Once these files are created, you can use your Model to interact with your tables very easily.
Step 4: List the contacts in our Block
Remember, in the previous tutorial we create a block in app/code/local/Pfay/Films/Block named Monblock.php. We will modify it to display the list of movies in our database.
First, insert a few lines in your pfay_films table via SQL. Then change the file Monblock.php as follows:
class Pfay_Films_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
//on initialize la variable
$retour='';
/* we made a request: pick up all the elements of the pfay_films table (thanks to our model pfay_films/film and sort by id_pfay_films */
$collection = Mage::getModel('pfay_films/film')
->getCollection()
->setOrder('id_pfay_films','asc');
/* then browse the result of the request and with the getData() function is stored in the variable return (for display in the template) the necessary data */
foreach($collection as $data)
{
$retour .= $data->getData('name.').'<br />';
}
/* I return a success message to the user (just so you know how to use the function) */
Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
return $retour;
}
}
Looking at the code, I think you will soon understand what we did with the input, if this is not the case please leave a comment.
Now test your code by going to http://votresite.com/Films/index and you see now display your movie list.
Your work :
Try to repeat a module creation from scratch before starting the next tutorial you need to know perfectly the first 4 tutorials before you can start following serenely.
Train yourself REALLY, traing at creating a new module to manage your contacts library for example. It is very similar to what we just did, but it's not the same so you will be able to practice and really understand what you are doing.
If that does not work? Feel free to download my code at the bottom of this article to compare with yours.
In the next tutorial, we get back a little more detail on how the collections are working so you can quickly make more complex operations on your database.
That is the end of this tutorial, I hope this tutorial helps you, now you know how to interract with the Magento database. Feel free to leave comments if you have questions.