J3.x

Difference between revisions of "Creating a simple module/Using the Database/fr"

From Joomla! Documentation

< J3.x:Creating a simple module
(Created page with "La balise ''uninstall'' supprime la table de base de données si le module est désinstallé. Notez que tous les modules n'utiliseront pas cette fonctionnalité (elle n'est pa...")
Line 31: Line 31:
 
3 sections sont présentes dans ce code :
 
3 sections sont présentes dans ce code :
 
*La balise "install" ajoute la table de base de données.
 
*La balise "install" ajoute la table de base de données.
*The ''uninstall'' tag removes the database table if the module is uninstalled. Note that not all modules will want to use this feature (and it's not required).
+
*La balise ''uninstall'' supprime la table de base de données si le module est désinstallé. Notez que tous les modules n'utiliseront pas cette fonctionnalité (elle n'est pas obligatoire).
 
*The ''update'' tag will update the databases if a database needs to be amended when updating the module.
 
*The ''update'' tag will update the databases if a database needs to be amended when updating the module.
  

Revision as of 10:22, 3 July 2015

Other languages:
Bahasa Indonesia • ‎English • ‎Nederlands • ‎español • ‎français • ‎português • ‎português do Brasil • ‎русский • ‎中文(台灣)‎
Joomla! 
3.x
Didacticiel
Création d'un module simple

Ceci est une série de plusieurs articles expliquant la façon de développer un module pour Joomla! Version Joomla 3.x. Vous pouvez naviguer dans les articles de cette série à l'aide du menu déroulant.

Commencez par l'Introduction puis naviguez dans les articles de cette série en utilisant soit les boutons de navigation situés en bas des articles, soit le menu droit (Les articles de cette série).




De nombreux de modules dans Joomla! nécessitent l'utilisation d'une base de données. Dans ce didacticiel, nous partons du principe que vous connaissez les bases de l'utilisation de la classe JDatabase. Si ce n'est pas le cas, veuillez préalablement consulter accéder à la base de données en utilisant JDatabase.

Création d'une table lors de l'installation

Afin de créer une table xml lors de l'installation, nous allons ajouter les lignes suivantes dans mod_helloworld.xml :

<install>
     <sql>
         <file driver="mysql" charset="utf8">sql/mysql/install.mysql.utf8.sql</file>
         <file driver="sqlazure" charset="utf8">sql/sqlazure/install.sqlazure.utf8.sql</file>
     </sql>
</install>

<uninstall>
     <sql>
         <file driver="mysql" charset="utf8">sql/mysql/uninstall.mysql.utf8.sql</file>
         <file driver="sqlazure" charset="utf8">sql/sqlazure/uninstall.sqlazure.utf8.sql</file>
     </sql>
</uninstall>

<update> 
    <schemas>
        <schemapath type="mysql">sql/mysql/updates</schemapath> 
	<schemapath type="sqlazure">sql/sqlazure/updates</schemapath> 
    </schemas> 
</update>

3 sections sont présentes dans ce code :

  • La balise "install" ajoute la table de base de données.
  • La balise uninstall supprime la table de base de données si le module est désinstallé. Notez que tous les modules n'utiliseront pas cette fonctionnalité (elle n'est pas obligatoire).
  • The update tag will update the databases if a database needs to be amended when updating the module.

Note that we have both schemas for MySQL and Microsoft SQL - again you can choose to tailor your module for one or both of these systems.

In this example we will just show the example files for the MySQL database. Creating the Microsoft SQL Server will be left as an exercise for the reader.

In our install.mysql.utf8.sql file we will create the table and place some hellos into it.

CREATE TABLE IF NOT EXISTS `#__helloworld` (
	`id` int(10) NOT NULL AUTO_INCREMENT,
	`hello` text NOT NULL,
	`lang` varchar(25) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

INSERT INTO `#__helloworld` (`hello`, `lang`) VALUES ('Hello World', 'en-GB');
INSERT INTO `#__helloworld` (`hello`, `lang`) VALUES ('Hola Mundo', 'es-ES');
INSERT INTO `#__helloworld` (`hello`, `lang`) VALUES ('Bonjour tout le monde', 'fr-FR');

Dans le fichier de désinstallation, nous allons simplement supprimer la table.

DROP TABLE IF EXISTS `#__helloworld`

Finally we'll just leave a placeholder in the updates file. There is an SQL file for each component version. Each file name must match the version string in the manifest file for that version. Joomla uses this string to determine which SQL files(s) to execute, and in what order they will be executed.

Important Note: These files are also used to set the version number in the #__schemas table. This version number must be present in the current version of the component in order for the new SQL files to be run during the update. For example, if you have version 1.0 and are updating to version 1.1, the 1.1.sql file will not be executed if there was no 1.0.sql file in the 1.0 release. For this reason, it is good practice to have a SQL update file for each version, even if there is no SQL change in that version.

# Placeholder file for database changes for version 1.0.0

Making the request in the helper file

Now on installing our module we should find that there is a helloworld database set up in our database schema with our hello's in. We must now retrieve this from the database to display to the user. We will now amend the getHello function we placed in the helper file in the last part.

For now we'll ignore using form fields to choose a hello and just retrieve the English shout.

// Obtain a database connection
$db = JFactory::getDbo();
// Retrieve the shout
$query = $db->getQuery(true)
            ->select($db->quoteName('hello'))
            ->from($db->quoteName('#__helloworld'))
            ->where('lang = ' . $db->Quote('en-GB'));
// Prepare the query
$db->setQuery($query);
// Load the row.
$result = $db->loadResult();
// Return the Hello
return $result;

Conclusion

Using modules with database connections for Joomla! is a fairly simple, straightforward process. Using the techniques described in this tutorial, a lot of modules can be developed with little hassle, with updates easy to manage.