Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

006. Using the database in the backend

Astrid edited this page Jan 4, 2020 · 9 revisions

Using the database in the backend

In this chapter we will ...

Now, we want to set up a database table.

t6_1

Newly created or Modified files

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.

changelog.xml

Comparing t5 t6 · astridx boilerplate

foo_update.xml

Comparing t5 t6 · astridx boilerplate(1)

administrator/components/com_foos/View/Foos/HtmlView.php

Comparing t5 t6 · astridx boilerplate(5)

administrator/components/com_foos/foos.xml

Comparing t5 t6 · astridx boilerplate(2)

administrator/components/com_foos/services/provider.php

Comparing t5 t6 · astridx boilerplate(3)

administrator/components/com_foos/tmpl/foos/default.php

Comparing t5 t6 · astridx boilerplate(4)

All changes at a glance

https://github.com/astridx/boilerplate/compare/t5...t6

More detailed explanations

File Structure

Newly created files

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

Modified files

changelog.xml

foo_update.xml

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

Example in Joomla 4

Side Note

Test your component

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 - not only edit the files - because the so Joomla creates the database table for you. Alternatively, you can create the database table yourself.

Concluding Remark

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.

Overview of all files