会员登录

联系我们

电话:18921289006

传真:0510-88804819

EMAIL: 这个 E-mail 受所垃圾邮件程序保护,您需要启用 JavaScript 才能查看。

开发手记

开发MVC组件之三-数据库

打印 Email

 

<p>在前面两部分,已经了解了怎么建立一个简单MVC模式的组件.之前视图从模块取得数据,而模块的数据就是一个硬编码字符,而这一节,将要数据库里的数据取而代之.了解模型如何用JClass类从数据库的表取得数据.<br /> 之前的模型已有经了一个方法:getGreeting(),这个方法非常简单,返回一个硬编码字符的问候.<br /> 为了使看起来更好了解一些,我们将从数据库载入一个问候.稍候再介绍如何建立一个数据库文件并在xml添加适当的代码以使表和一些示例数据在组件安装时添加到数据库去.现在先简单替换掉返回声明以直接的从数据库检索数据并返回它.<br /> 第一步是获得一个数据库的对象,自从Joomla!有这个数据作为它的正常的运作,数据库的连接实际上已经存在.因此你就没有必要自己再建立一个连接.引用一个数据库可以用以下代码:<br /> <span class="re0">$db</span> =&amp; JFactory::<span class="me2">getDBO</span><span class="br0">(</span><span class="br0">)</span>;<br /> JFactory是一个静态的类,用来检索引用大量的系统的对象.方法名 (getDBO) 是为了得到一个数据库对象,记住它非常简单但也是非常重要.<br /> 现在已经获得一个数据库对象,下一步就可以从检索数据.<br /> 第一步就是将查询语句存在对象中<br /> 第二步载入结果<br /> 新的getGreeting() 方法于是看起来就应该像这样:</p>
<pre class="source-php"><span class="kw2">function</span> getGreeting<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
   <span class="re0">$db</span> =&amp; JFactory::<span class="me2">getDBO</span><span class="br0">(</span><span class="br0">)</span>;
 
   <span class="re0">$query</span> = <span class="st0">'SELECT greeting FROM #__hello'</span>;
   <span class="re0">$db</span>-&gt;<span class="me1">setQuery</span><span class="br0">(</span> <span class="re0">$query</span> <span class="br0">)</span>;
   <span class="re0">$greeting</span> = <span class="re0">$db</span>-&gt;<span class="me1">loadResult</span><span class="br0">(</span><span class="br0">)</span>;
 
   <span class="kw1">return</span> <span class="re0">$greeting</span>;
<span class="br0">}</span></pre>
<p>hello是一会将要建的数据库的表的名, greeting是是存在表里的字段的名称.<br /> $db-&gt;loadResult()方法将会执行查询并将返回第一个字段的第一行的数据结果.</p>
<h2><span class="mw-headline">建立一个安装的数据库文件</span></h2>
<p>是oomla安装已经嵌入一个可以在安装组件期间支持执行查询,这些查询都存在标准的文本文件里.我们将有三个查询在我们的安装文件里,首先如果是数据库已经存在这个表就将之删除,第二建立一个含用适当字段的表,第三插入数据.<br /> 如下:</p>
<pre class="source-php">DROP TABLE <span class="kw1">IF</span> EXISTS `<span class="co2">#__hello`;</span>
 
CREATE TABLE `<span class="co2">#__hello` (</span>
  `id` int<span class="br0">(</span><span class="nu0">11</span><span class="br0">)</span> NOT <span class="kw2">NULL</span> auto_increment,
  `greeting` varchar<span class="br0">(</span><span class="nu0">25</span><span class="br0">)</span> NOT <span class="kw2">NULL</span>,
  PRIMARY <span class="kw3">KEY</span>  <span class="br0">(</span>`id`<span class="br0">)</span>
<span class="br0">)</span> ENGINE=MyISAM AUTO_INCREMENT=<span class="nu0">0</span> <span class="kw2">DEFAULT</span> CHARSET=utf8;
 
INSERT INTO `<span class="co2">#__hello` (`greeting`) VALUES ('Hello, World!'),</span>
<span class="br0">(</span><span class="st0">'Bonjour, Monde!'</span><span class="br0">)</span>,
<span class="br0">(</span><span class="st0">'Ciao, Mondo!'</span><span class="br0">)</span></pre>
<p>可能你会觉得这些表的前辍非常奇怪,其实joomla会用现在的前辍替换掉它,一般都是jos_,安装你可以设定前辍,这样就可以在一个数据库安装几个不同的joomla数据.<br /> 将这些语句存储在 install.utf.sql文本文件里.</p>
<h3><span class="mw-headline">建立一个卸载时的SQL文件</span></h3>
<p><br /> 这个只需要一个查询语句即可:</p>
<pre class="source-php">DROP TABLE <span class="kw1">IF</span> EXISTS `<span class="co2">#__hello`;</span>
存储在uninstall.utf.sql文件里.
 
</pre>
<p><br /> 升级一下安装文件<br /> 我们需要对xml安装文件作些变动,第一步我们需要添加以上的两个文件以便安装.SQL安装要放在管理文件夹里,第二就是在安装时通知安装程序执行查询语句,<br /> 示例如下:</p>
<pre class="source-xml"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">"1.0"</span> <span class="re0">encoding</span>=<span class="st0">"utf-8"</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;install</span> <span class="re0">type</span>=<span class="st0">"component"</span> <span class="re0">version</span>=<span class="st0">"1.5.0"</span><span class="re2">&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;name<span class="re2">&gt;</span></span></span>Hello<span class="sc3"><span class="re1">&lt;/name<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- The following elements are optional and free of formatting conttraints --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;creationDate<span class="re2">&gt;</span></span></span>2007-02-22<span class="sc3"><span class="re1">&lt;/creationDate<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;author<span class="re2">&gt;</span></span></span>John Doe<span class="sc3"><span class="re1">&lt;/author<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;authorEmail<span class="re2">&gt;</span></span></span> 这个 E-mail 受所垃圾邮件程序保护,您需要启用 JavaScript 才能查看。 <span class="sc3"><span class="re1">&lt;/authorEmail<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;authorUrl<span class="re2">&gt;</span></span></span>http://www.example.org<span class="sc3"><span class="re1">&lt;/authorUrl<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;copyright<span class="re2">&gt;</span></span></span>Copyright Info<span class="sc3"><span class="re1">&lt;/copyright<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;license<span class="re2">&gt;</span></span></span>License Info<span class="sc3"><span class="re1">&lt;/license<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!--  The version string is recorded in the components table --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>3.01<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- The description is optional and defaults to the name --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;description<span class="re2">&gt;</span></span></span>Description of the component ...<span class="sc3"><span class="re1">&lt;/description<span class="re2">&gt;</span></span></span>
 
 <span class="sc3"><span class="coMULTI">&lt;!-- Site Main File Copy Section --&gt;</span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- Note the folder attribute: This attribute describes the folder<br />      to copy FROM in the package to install therefore files copied<br />      in this section are copied from /site/ in the package --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;files</span> <span class="re0">folder</span>=<span class="st0">"site"</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>controller.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>models/hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>models/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/view.html.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/tmpl/default.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/tmpl/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/files<span class="re2">&gt;</span></span></span>
 
 <span class="sc3"><span class="re1">&lt;install<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;sql<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;file</span> <span class="re0">charset</span>=<span class="st0">"utf8"</span> <span class="re0">driver</span>=<span class="st0">"mysql"</span><span class="re2">&gt;</span></span>install.sql<span class="sc3"><span class="re1">&lt;/file<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/sql<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/install<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;uninstall<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;sql<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;file</span> <span class="re0">charset</span>=<span class="st0">"utf8"</span> <span class="re0">driver</span>=<span class="st0">"mysql"</span><span class="re2">&gt;</span></span>uninstall.sql<span class="sc3"><span class="re1">&lt;/file<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/sql<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/uninstall<span class="re2">&gt;</span></span></span>
 
 <span class="sc3"><span class="re1">&lt;administration<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="coMULTI">&lt;!-- Administration Menu Section --&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;menu<span class="re2">&gt;</span></span></span>Hello World!<span class="sc3"><span class="re1">&lt;/menu<span class="re2">&gt;</span></span></span>
 
  <span class="sc3"><span class="coMULTI">&lt;!-- Administration Main File Copy Section --&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;files</span> <span class="re0">folder</span>=<span class="st0">"admin"</span><span class="re2">&gt;</span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>install.sql<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>uninstall.sql<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/files<span class="re2">&gt;</span></span></span>  
 
 <span class="sc3"><span class="re1">&lt;/administration<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/install<span class="re2">&gt;</span></span></span>
 
 
</pre>
<p><br /> <br /></p>