-
Notifications
You must be signed in to change notification settings - Fork 30
006. Using the database in the backend
Now, we want to set up a database table.
Newly created files
administrator/components/com_foos/Model/FoosModel.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_foos
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Foos\Administrator\Model;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\Model\ListModel;
/**
* Methods supporting a list of foo records.
*
* @since 1.0
*/
class FoosModel extends ListModel
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see \JControllerLegacy
* @since 1.0
*/
public function __construct($config = array())
{
parent::__construct($config);
}
/**
* Build an SQL query to load the list data.
*
* @return \JDatabaseQuery
*
* @since 1.0
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select(
$db->quoteName(array('id', 'name'))
);
$query->from($db->quoteName('#__foos_details'));
return $query;
}
}
administrator/components/com_foos/sql/install.mysql.utf8.sql
CREATE TABLE IF NOT EXISTS `#__foos_details` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`alias` varchar(400) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '',
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci;
INSERT INTO `#__foos_details` (`name`) VALUES
('Nina'),
('Astrid'),
('Elmar');
administrator/components/com_foos/sql/uninstall.mysql.utf8.sql
DROP TABLE IF EXISTS `#__foos_details`;
Modified files
We need to modify our installation XML file, the update server and the change log to include the new version.
administrator/components/com_foos/View/Foos/HtmlView.php
administrator/components/com_foos/foos.xml
administrator/components/com_foos/services/provider.php
administrator/components/com_foos/tmpl/foos/default.php
https://github.com/astridx/boilerplate/compare/t5...t6
administrator/components/com_foos/Model/FoosModel.php
administrator/components/com_foos/sql/install.mysql.utf8.sql
administrator/components/com_foos/sql/uninstall.mysql.utf8.sql
administrator/components/com_foos/View/Foos/HtmlView.php
administrator/components/com_foos/foos.xml
administrator/components/com_foos/services/provider.php
administrator/components/com_foos/tmpl/foos/default.php
Now you can zip all files and install them via Joomla Extension Manager. After that you can see a link to your component in the left side menu. Clicking on this link will open the basic back end view.
Now you should do a new installation of the component, so you can import changes later under database.
Now our component has a database table where it can store data. In this chapter we have seen, how we can display this data in the back end. In the next chapter we will go on an check, how we can use the data in the front end.
Now we have . Up to now we have no . We are going to work on this in the next chapter.