南荒工作室

开发MVC组件之三-数据库

   在前面两部分,已经了解了怎么建立一个简单MVC模式的组件.之前视图从模块取得数据,而模块的数据就是一个硬编码字符,而这一节,将要数据库里的数据取而代之.了解模型如何用JClass类从数据库的表取得数据.
    之前的模型已有经了一个方法:getGreeting(),这个方法非常简单,返回一个硬编码字符的问候.
为了使看起来更好了解一些,我们将从数据库载入一个问候.稍候再介绍如何建立一个数据库文件并在xml添加适当的代码以使表和一些示例数据在组件安装时添加到数据库去.现在先简单替换掉返回声明以直接的从数据库检索数据并返回它.
    第一步是获得一个数据库的对象,自从Joomla!有这个数据作为它的正常的运作,数据库的连接实际上已经存在.因此你就没有必要自己再建立一个连接.引用一个数据库可以用以下代码:
$db =& JFactory::getDBO();
JFactory是一个静态的类,用来检索引用大量的系统的对象.方法名 (getDBO) 是为了得到一个数据库对象,记住它非常简单但也是非常重要.
现在已经获得一个数据库对象,下一步就可以从检索数据.
第一步就是将查询语句存在对象中
第二步载入结果
新的getGreeting() 方法于是看起来就应该像这样:
function getGreeting()
{
   $db =& JFactory::getDBO();
 
   $query = 'SELECT greeting FROM #__hello';
   $db->setQuery( $query );
   $greeting = $db->loadResult();
 
   return $greeting;
}
hello是一会将要建的数据库的表的名, greeting是是存在表里的字段的名称.
$db->loadResult()方法将会执行查询并将返回第一个字段的第一行的数据结果.

建立一个安装的数据库文件

是oomla安装已经嵌入一个可以在安装组件期间支持执行查询,这些查询都存在标准的文本文件里.我们将有三个查询在我们的安装文件里,首先如果是数据库已经存在这个表就将之删除,第二建立一个含用适当字段的表,第三插入数据.
如下:
DROP TABLE IF EXISTS `#__hello`;
 
CREATE TABLE `#__hello` (
  `id` int(11) NOT NULL auto_increment,
  `greeting` varchar(25) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
 
INSERT INTO `#__hello` (`greeting`) VALUES ('Hello, World!'),
('Bonjour, Monde!'),
('Ciao, Mondo!')
可能你会觉得这些表的前辍非常奇怪,其实joomla会用现在的前辍替换掉它,一般都是jos_,安装你可以设定前辍,这样就可以在一个数据库安装几个不同的joomla数据.
将这些语句存储在 install.utf.sql文本文件里.

建立一个卸载时的SQL文件


这个只需要一个查询语句即可:
DROP TABLE IF EXISTS `#__hello`;
存储在uninstall.utf.sql文件里.


升级一下安装文件
我们需要对xml安装文件作些变动,第一步我们需要添加以上的两个文件以便安装.SQL安装要放在管理文件夹里,第二就是在安装时通知安装程序执行查询语句,
示例如下:
<?xml version="1.0" encoding="utf-8"?>
<install type="component" version="1.5.0">
 <name>Hello</name>
 <!-- The following elements are optional and free of formatting conttraints -->
 <creationDate>2007-02-22</creationDate>
 <author>John Doe</author>
 <authorEmail>
  该E-mail地址已受到防止垃圾邮件机器人的保护,您必须启用浏览器的Java Script才能看到。
 </authorEmail>
 <authorUrl>http://www.example.org</authorUrl>
 <copyright>Copyright Info</copyright>
 <license>License Info</license>
 <!--  The version string is recorded in the components table -->
 <version>3.01</version>
 <!-- The description is optional and defaults to the name -->
 <description>Description of the component ...</description>
 
 <!-- Site Main File Copy Section -->
 <!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site"> <filename>controller.php</filename> <filename>hello.php</filename> <filename>index.html</filename> <filename>models/hello.php</filename> <filename>models/index.html</filename> <filename>views/index.html</filename> <filename>views/hello/index.html</filename> <filename>views/hello/view.html.php</filename> <filename>views/hello/tmpl/default.php</filename> <filename>views/hello/tmpl/index.html</filename> </files> <install> <sql> <file charset="utf8" driver="mysql">install.sql</file> </sql> </install> <uninstall> <sql> <file charset="utf8" driver="mysql">uninstall.sql</file> </sql> </uninstall> <administration> <!-- Administration Menu Section --> <menu>Hello World!</menu> <!-- Administration Main File Copy Section --> <files folder="admin"> <filename>hello.php</filename> <filename>index.html</filename> <filename>install.sql</filename> <filename>uninstall.sql</filename> </files> </administration> </install>


feed0 Comments

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy
最后更新 ( 2009-04-22 15:15 )  

案例展示

LiveZilla Live Help