### Creating and managing Joomla database (b2_database)
With the new git branch **b2_database** we continue our workflow.
Joomla usually manage its content with a database. In our component, we use
a MariaDB database. At the time of development of this component we have PHP8.2,
and we use Mariadb database with version from 11.1,
client 15.2 for Linux (x86_64) using readline 5.1
We support MySQL and MariaDB; not tested support for PostgreSQL.
In the manifest file \<component_name\>.xml, here the file **depot.xml**,
there are installation instructions to "install", "uninstall", and
"update" the Joomla extension/component.
The SQL plain text files are stored in and as:
- admin/sql/
- install.mysql.utf8.sql
- uninstall.mysql.utf8.sql
- admin/sql/updates/mysql/
- 0.0.1.sql
- 0.0.4.sql
- 0.0.5.sql
#### Database table installation
1. When the component is installed for the first time, the file
**install.mysql.utf8.sql** is executed.
1. If the component is already installed, the update
scenario comes into play, the folder **admin/sql/updates/mysql/**: Only the files with higher version
numbers than the installed version of the component will be
executed in ascending order.<br>
Hint: The version of the installed component is stored in the
Joomla's table "#__schemas".
The "#__"-prefix is substituted automatically with the database prefix, (e.g. "jm_") which is defined in the configuration file (configuration.php) of the installed Joomla version as parameter $dbprefix.
**install.mysql.utf8.sql**
```sql
DROP TABLE IF EXISTS `#__depot`;
CREATE TABLE `#__depot`(
`id` SERIAL,
`component_name` VARCHAR(1024) CHARACTER SET ascii COLLATE ascii_general_ci NULL DEFAULT NULL
COMMENT 'unique component name (ASCII characters only)',
`alias` VARCHAR(1024) NOT NULL DEFAULT '',
`description` VARCHAR(4000) NOT NULL DEFAULT '',
`quantity` INT(10) UNSIGNED NOT NULL DEFAULT 0,
`quantity_exp` INT(11) NOT NULL DEFAULT 0
COMMENT 'Exponent of the quantity (10^x of the number, usually 0 i.e. 10⁰)',
`asset_id` INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'FK to the #__assets table.',
`created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(10) UNSIGNED NOT NULL DEFAULT 0,
`checked_out` INT(11) NOT NULL DEFAULT 0,
`checked_out_time` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` INT(10) UNSIGNED NOT NULL DEFAULT 0,
('1N4148','1n4148','diode, general purpose',1234,'2023-09-25 15:15:15',2,1,2);
```
The table is created in the database when the component is installed.
Also two lines of sample data are inserted into the table "#__depot".
#### Database table uninstallation
When the component is uninstalled the
**uninstall.mysql.utf8 sql** is executed.
We drop the used tables from the database.
**install.mysql.utf8.sql**
```sql
DROP TABLE IF EXISTS `#__depot`;
```
#### Database table update
When the component is updated the
**admin/sql/updates/mysql** folder with its files is executed.
Even if you do not need a database update, you can add an empty file in admin/sql/updates/mysql/0.0.2.sql to initialize the schema version.
In future versions, if you plan to use database tables, the update can be performed automatically.
We create an empty file, just with a comment "-- version 0.0.2"
**admin/sql/updates/mysql/0.0.2.sql**
```sql
-- version 0.0.2
```
#### Manifest file for extensions
The sql files are only executed if they exist in the \<component_name\>.xml manifest file. We add this after the namespace tag, just before the files information tag:
public function getForm($data = array(), $loadData = true)
{
$form = $this->loadForm('com_depot.part',
'part', array('control' => 'jform',
'load_data' => $loadData));
if (empty($form)) {
return false;
}
return $form;
}
}
```
The **loadForm()** and **preprocessForm()** methods are defined in
the FromModel class and the **bind()** method is defined in the
Form class. The first argument (name) of loadForm() is set to
"com_depot.part". The second argument (form xml source) is "part",
and the third argument is the associative array for options.
The form is defined in the source file: **forms/part.xml**
After you have set the $form variable with the Form object,
you check to see if you are loading data to the form.
If you want to pre-load data for the form, you include an element
called "load_data" that is set to a boolean true.
Then, the method calls the loadFormData() method to get the data
for the form. This method gets any previously loaded data from
the session or database.
###### Modifying Form dynamically
Inside the getForm() method, before returning the $form, you can modify the form with many methods of the Form class. You can easily fine-tune your forms dynamically before they are rendered.
#### Layout file - rendering Form
**tmpl/part/edit.php**
After you get the form in the view file ($this->form), the form
The display() calls this method to include the toolbar.
```php
public function display($tpl = null)
{
$this->form = $this->get('Form');
$this->addToolbar();
parent::display($tpl);
}
```
#### Controller file
**admin/src/Controller/PartController.php***
Now, when these action buttons are clicked, Joomla will look for apply(), save() or cancel()
methods in the Part controller. So, create a controller clss that will extend FormController.
These methods are already defined in the parent class.
```php
use Joomla\CMS\MVC\Controller\FormController;
class PartController extends FormController
{
}
```
#### Model file
**admin/src/model/PartModel.php**
If you click on Save button, it will save the data and redirect to the editing screen. However, the data will not be present in the form. Though, we have set `$loadData` to true, we also need to create a method `loadFormData()` to get the data for the form.