Het ontwikkelen van een MVC component - De database gebruiken
From Joomla! Documentation
< J3.x:Developing an MVC Component
Artikelen in deze reeks
Het toevoegen van een menu aan de website
Het toevoegen van een model aan de website
Een 'variable request' toevoegen in het menu type
De database gebruiken
Basis beheergedeelte
Het toevoegen van taalbeheer
Het toevoegen van acties in het beheergedeelte
Het toevoegen van decoraties aan het beheergedeelte
Het toevoegen van controles
Het toevoegen van categorieën
Het toevoegen van configuratie
Het toevoegen van een installatie/deïnstallatie/update script-bestand
Een formulier op de website toevoegen
Het gebruik van de taal filterfunctie
- Een modaal venster toevoegen
- Het toevoegen van associaties
- Het toevoegen van uitchecken
- Het toevoegen van volgorde
- Het toevoegen van niveaus
- Het toevoegen van versies
- Het toevoegen van tags
- Het toevoegen van rechten
- Het toevoegen van batch-proces
- Het toevoegen van cache
- Het toevoegen van een feed
Het toevoegen van een update-server
Dit is een reeks van artikelen met handleidingen over het ontwikkelen van een Model-View-Controller Component voor Joomla! versie.
Begin met de Introductie en navigeer door de artikelen van de reeks door middel van de navigatieknop onderaan of het vak rechts (Artikelen in deze reeks).
Inleiding
Deze handleiding is een onderdeel van de Het ontwikkelen van een MVC Component voor Joomla! 3.3 uitleg. Het is aan te raden om de vorige delen van deze handleiding te lezen voordat u dit onderdeel leest.
De database gebruiken
Componenten beheren gewoonlijk hun inhoud met behulp van de database. Tijdens de installatie/de-installatie fase van een component, kunt u SQL query's uitvoeren met behulp van SQL-tekstbestanden.
Maak met uw voorkeur editor twee bestanden aan genaamd admin/sql/install.mysql.utf8.sql en admin/sql/updates/mysql/0.0.6.sql. Ze moeten beide dezelfde inhoud heb:
admin/sql/install.mysql.utf8.sql and admin/sql/updates/mysql/0.0.6.sql
DROP TABLE IF EXISTS `#__helloworld`;
CREATE TABLE `#__helloworld` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`greeting` VARCHAR(25) NOT NULL,
`published` tinyint(4) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE =MyISAM
AUTO_INCREMENT =0
DEFAULT CHARSET =utf8;
INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Good bye World!');
Het bestand install.mysql.utf8.sql zal worden uitgevoerd als u deze component installeert. Het bestand 0.0.6.sql wordt uitgevoerd als u een update doet.
Belangrijke opmerking: Als de component geïnstalleerd is, worden de bestanden in de SQL updates map (bijvoorbeeld, admin/sql/updates/mysql) gelezen en de naam van het laatste bestand wordt gebruikt om het versienummer van de component te plaatsen in de #__schemas
tabel. Deze waarde moet in deze tabel staan ten behoeve van automatische uitvoering van de update-SQL bestanden van toekomstige versies. Hiervoor is het goede gewoonte om een SQL-update bestand te maken voor iedere versie (zelfs als hij leeg is of alleen commentaar bevat). Op deze manier komt de #__schemas
versie altijd overeen met de versie van de component.
Belangrijke opmerking: Wees zeker dat u de SQL-bestanden opslaat als utf8 NOT BOM, anders mislukt de query met MySQL error #1064.
Dit is het installatie bestand. Het wordt uitgevoerd als er een juist commando in het helloworld.xml bestand staat.
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2014</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</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>0.0.6</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<install> <!-- Runs on install -->
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
<uninstall> <!-- Runs on uninstall -->
<sql>
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- 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>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
<!-- Administration 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 /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
</files>
</administration>
</extension>
Doe hetzelfde voor het deinstallatie bestand:
Plaats met uw favoriete editor een bestand admin/sql/uninstall.mysql.utf8.sql dat bevat:
admin/sql/uninstall.mysql.utf8.sql
DROP TABLE IF EXISTS `#__helloworld`;
Het toevoegen van een nieuw veldtype
We hebben tot nu toe een hard gecodeerd veldtype voor berichten gebruikt. We moeten onze database gebruiken om het beicht te kiezen.
Wijzig het site/views/helloworld/tmpl/default.xml bestand en plaats deze regels
site/views/helloworld/tmpl/default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
</layout>
<fields
name="request"
addfieldpath="/administrator/components/com_helloworld/models/fields"
>
<fieldset name="request">
<field
name="id"
type="helloworld"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
/>
</fieldset>
</fields>
</metadata>
Het introduceert een nieuw veldtype en verteld Joomla naar de velddefinitie te kijken in de /administrator/components/com_helloworld/models/fields map.
Plaats, met uw favoriete editor een bestand admin/models/fields/helloworld.php dat bevat:
admin/models/fields/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JFormHelper::loadFieldClass('list');
/**
* HelloWorld Form Field class for the HelloWorld component
*
* @since 0.0.1
*/
class JFormFieldHelloWorld extends JFormFieldList
{
/**
* The field type.
*
* @var string
*/
protected $type = 'HelloWorld';
/**
* Method to get a list of options for a list input.
*
* @return array An array of JHtml options.
*/
protected function getOptions()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id,greeting');
$query->from('#__helloworld');
$db->setQuery((string) $query);
$messages = $db->loadObjectList();
$options = array();
if ($messages)
{
foreach ($messages as $message)
{
$options[] = JHtml::_('select.option', $message->id, $message->greeting);
}
}
$options = array_merge(parent::getOptions(), $options);
return $options;
}
}
Het nieuwe veldtype toont een drop-down lijst met berichten waaruit gekozen kan worden. U kunt het resultaat van deze wijziging zien in de menubeheer sectie van het helloworld item.
Toon het gekozen bericht
Indien een menu-item van deze component aangemaakt/bijgewerkt wordt, slaat Joomla de ID van het bericht op. Het HelloWorldModelHelloWorld model moet nu het bericht berekenen aan de hand van de ID en de gegevens die in de database zijn opgeslagen.
Wijzig het site/models/helloworld.php bestand:
site/models/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HelloWorld Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var array messages
*/
protected $messages;
/**
* Method to get a table object, load it if necessary.
*
* @param string $type The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A JTable object
*
* @since 1.6
*/
public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Get the message
*
* @param integer $id Greeting Id
*
* @return string Fetched String from Table for relevant Id
*/
public function getMsg($id = 1)
{
if (!is_array($this->messages))
{
$this->messages = array();
}
if (!isset($this->messages[$id]))
{
// Request the selected id
$jinput = JFactory::getApplication()->input;
$id = $jinput->get('id', 1, 'INT');
// Get a TableHelloWorld instance
$table = $this->getTable();
// Load the message
$table->load($id);
// Assign the message
$this->messages[$id] = $table->greeting;
}
return $this->messages[$id];
}
}
Het model vraagt de TableHelloWorld nu om het bericht op te halen. Deze table class moet gedefinieerd zijn in het admin/tables/helloworld.php bestand
admin/tables/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
/**
* Hello Table class
*
* @since 0.0.1
*/
class HelloWorldTableHelloWorld extends JTable
{
/**
* Constructor
*
* @param JDatabaseDriver &$db A database connector object
*/
function __construct(&$db)
{
parent::__construct('#__helloworld', 'id', $db);
}
}
You shouldn't see any differences, but if you access the database you should see a table named jos_helloworld with two columns: id and greeting. And two entries: Hello World! and Good bye World.
Packaging the component
Content of your code directory
- helloworld.xml
- site/helloworld.php
- site/index.html
- site/controller.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.xml
- site/views/helloworld/tmpl/default.php
- site/models/index.html
- site/models/helloworld.php
- admin/index.html
- admin/helloworld.php
- admin/sql/index.html
- admin/sql/install.mysql.utf8.sql
- admin/sql/uninstall.mysql.utf8.sql
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
- admin/sql/updates/mysql/0.0.6.sql
- admin/models/index.html
- admin/models/fields/index.html
- admin/models/fields/helloworld.php
- admin/tables/index.html
- admin/tables/helloworld.php
Create a compressed file of this directory or directly download the archive and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code discrepancies or if editing any of the source code on this page.