Updating Data in Multiple Tables Within a Single DGS
In DGS, there's a concept of a main table (typically specified in the table
attribute name
) and additional (child) tables (specified in the store
attributes in fields).
Creating DGS with Multiple Tables
Very often, you need to simultaneously edit and add data to different tables in the database. For example, imagine we have an employee table and a table for feedback on them, so we have two tables: employees and employee_feedbacks, but in DGS, editing and adding should happen as if it's a single table. To solve this task, we need to use the store
attribute in Field
and routers
:
<?xml version="1.0" encoding="UTF-8" ?>
<table name="employee_feedbacks"
charset="UTF-8"
primaryKey="id"
defaultOrderField="id"
defaultOrderDirection="DESC"
exceptionMode="true"
rowsForPage="20">
<routers>
<route store="employee_feedbacks"
joinStore="employees"
type="INNER"
on="employee_feedbacks.id_employee = employees.id" />
</routers>
<fields>
<field type="text"
caption="<?php echo __l('First Name'); ?>"
name="first_name"
store="employees"
width="30%"
required="true" />
<field type="text"
caption="<?php echo __l('Last Name'); ?>"
name="last_name"
store="employees"
width="30%"
required="true" />
<field type="textarea"
caption="<?php echo __l('Feedback'); ?>"
name="note"
hide="true"
required="true" />
</fields>
<externalValues>
<value field="id_author"><?php echo Core::getInstance()->user->getID(); ?></value>
</externalValues>
<actions>
<action type="list"
isLoadAllColumns="true"
caption="<?php echo __('Feedbacks'); ?>" />
<action type="insert"
caption="<?php echo __('Create New Feedback')?>"
title="<?php echo __('Create New Feedback'); ?>" />
<action type="edit"
caption="<?php echo __('Edit')?>"
title="<?php echo __('Edit Feedback'); ?>" />
<action type="remove"
caption="<?php echo __('Remove')?>" />
</actions>
</table>
Adding Values to Additional Tables in DGS
When you need to add additional values for adding/updating data in additional DGS tables,
you can use the Store::EVENT_PREPARE_REPOSITORY_VALUES
event:
$store->addEventListener(Store::EVENT_PREPARE_REPOSITORY_VALUES, function (FestiEvent &$event) {
$tablesValues = &$event->getTargetValueByKey('general');
$store = $event->getTargetValueByKey('store');
assert($store instanceof Store);
if ($store->getAction() == Store::ACTION_INSERT) {
$tablesValues['employees']['cdate'] = date('Y-m-d');
} else {
$tablesValues['employees']['mdate'] = date('Y-m-d H:i:s');
}
});
Adding and Editing Values Only in an Additional Table with Display Through the Main Table
<?xml version="1.0" encoding="UTF-8"?>
<table charset="UTF-8"
name="ticketing_systems"
primaryKey="id"
defaultOrderField="id"
defaultOrderDirection="DESC"
rowsForPage="50">
<routers>
<route store="ticketing_systems"
joinStore="ticketing_systems2users"
type="LEFT"
on="ticketing_systems.id = ticketing_systems2users.id_system AND ticketing_systems2users.id_user = <?php echo Utils::getUserID() ?>" />
</routers>
<fields>
<field type="text"
name="caption"
required="true"
caption="<?php echo __('Name'); ?>"
onlyList="true"
filter="text" />
<field type="text"
name="api_token"
store="ticketing_systems2users"
isnull="true"
caption="<?php echo __('Token'); ?>"
hide="true" />
</fields>
<actions>
<action type="list"
caption="<?php echo __('Services'); ?>"
isLoadAllColumns="true" />
<action type="edit"
caption="<?php echo __('Edit'); ?>"/>
</actions>
</table>