J3.x

建立一個簡單的模組/使用資料庫

From Joomla! Documentation

< J3.x:Creating a simple module
This page is a translated version of the page J3.x:Creating a simple module/Using the Database and the translation is 52% complete.

Other languages:
English • ‎español • ‎français • ‎Bahasa Indonesia • ‎Nederlands • ‎português • ‎português do Brasil • ‎русский • ‎中文(台灣)‎
Joomla! 
3.x
教學
建立一個簡單的模組

這一系列的文章介紹如何 建立一個 Joomla! 模組 版本 Joomla 3.x。您可以依序瀏覽文章。

讓我們從簡介開始,您可以使用底下的導覽按鈕來瀏覽文章,或是右側的方塊中的連結(列出所有的文章)。 您可以觀看 2 部和本教學相關的影片,基本 Joomla 模組開發影片教學 1 and 基本 Joomla 模組開發影片教學 2.




Joomla 中許多模組都會使用到資料庫,這個教學假定您已經了解使用 JDatabase class 的基礎知識。如果沒有,請在繼續本教學之前,閱讀 使用 JDatabase 來取用資料庫 教學文件內容。

在安裝過程建立資料表

要在安裝過程中建立 xml table,我們在 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>

這一段程式碼有三節:

  • install 標記新增了資料表。
  • 當模組解除安裝時,uninstall 標記用來移除資料表,請注意不是所有的模組會使用這個功能(它也不是必須的項目)。
  • 假使有必要的話,在更新模組時, update 標記會更新資料庫。

Next you should specify the folder tag in the files section to ensure that the sql files are copied from your installation zip package:

<folder>sql</folder>

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=INNODB 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');

In the uninstall file we'll just remove the 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;

結論

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.