Commit f8fde0ad authored by Melikhov Arsenii's avatar Melikhov Arsenii

Update useMany.md;

update MANY type description text
parent f4f68edd
......@@ -4,15 +4,15 @@
[SELECT to the Many attribute](#select-to-the-many-attribute)
<a name="endmenu"></a>
Imagine that we want to add to request information about the region of the city to which that request related for.
Imagine that we want to add to our request information about the region of the city to which that request related for.
Since one request can relate to several regions of the city, we need to have the ability of multiple choice.
For this, we need the `MANY` datatype.
Let's look at how to use it.
## Creating Many datatype attribute
First, create the entity that represents the city region - `req_cityRegion.meta` with code below.
First, we need to create the entity that represents the city region.
`req_cityRegion.meta`:
```javascript
{
"caption": "City region",
......@@ -37,47 +37,50 @@ First, create the entity that represents the city region - `req_cityRegion.meta`
}
```
Then, in the entity req_reqList( `req_reqList.meta` ) add an attribute of the datatype `MANY` that is associated with the entity req_cityRegion:
Then, in the `req_reqList` entity add an attribute of the datatype `MANY` that is associated with the `req_cityRegion` entity:
```javascript
{
"name":"regionList",
"dataType": "Many",
"associatedEntity": "req_cityRegion",
"associationManyData": "req_cityRegion_map",
"caption": "List city regions",
"description": "List of city regions",
"allowNull": true
"name":"regionList",
"dataType": "Many",
"associatedEntity": "req_cityRegion",
"associationManyData": "req_city_region_map",
"caption": "List city regions",
"description": "List of city regions",
"allowNull": true
}
```
After running the generateDDL script in the database, a `req_cityRegion_map` table will be created with the `sourceID(bigint)` and `destID(bigint)` fields, where the sourceID - ID of the req_reqList table, the destID - ID of the req_cityRegion table.
Create the script to fill the req_cityRegion table data.
`_initialData/040_fill_cityRegions.js`
After running the generateDDL, the `req_cityRegion_map` table will be created with two fields - `sourceID(bigint)` and `destID(bigint)`.
The `sourceID` is an ID of the `req_reqList` table and the `destID` is an ID of the `req_cityRegion` table.
Now, we will add the script for filling the `req_cityRegion` table.
`_initialData/040_fill_cityRegions.js`:
```javascript
module.exports = function(session){
const path = require('path')
const fs = require('fs')
const {dataLoader, csv, argv} = require('@unitybase/base')
let conn = session.connection;
let fn = path.join(__dirname, '/req_cityRegions.csv')
let fContent = fs.readFileSync(fn)
if (!fContent) { throw new Error(`File ${fn} is empty or not exist`) }
fContent = fContent.trim()
let csvData = csv.parse(fContent)
//check existing records in the DB
let notExisted = csvData.filter(
(row) => !conn.lookup('req_cityRegion', 'ID',
conn.Repository('req_cityRegion').where('name', '=', row[0]).ubql().whereList
)
)
console.info('\t\tFill City Region field (req_cityRegion)');
dataLoader.loadArrayData(conn, notExisted, 'req_cityRegion', 'name'.split(';'), [0], 1);
};
module.exports = function (session) {
const path = require('path')
const fs = require('fs')
const { dataLoader, csv, argv } = require('@unitybase/base')
const conn = session.connection
const fn = path.join(__dirname, '/req_cityRegions.csv')
let fContent = fs.readFileSync(fn)
if (!fContent) {
throw new Error(`File ${fn} is empty or not exist`)
}
fContent = fContent.toString('utf8').trim()
const csvData = csv.parse(fContent)
// check existing records in the DB
const notExisted = csvData.filter(
(row) => !conn.lookup('req_cityRegion', 'ID',
conn.Repository('req_cityRegion').where('name', '=', row[0]).ubql().whereList
)
)
console.info('\t\tFill City Region field (req_cityRegion)')
dataLoader.loadArrayData(conn, notExisted, 'req_cityRegion', 'name'.split(';'), [0], 1)
}
```
Regions name in `_initialData/req_cityRegions.csv`
Don't forget to put these regions names to `_initialData/req_cityRegions.csv`:
```text
central
southern
......@@ -86,43 +89,45 @@ west
eastern
```
Add City Region shortcut in `_initalData/010_create_navshortcuts.js`
We also need a shortcut for the City Region.
We will create this with `_initalData/010_create_navshortcuts.js`.
Add the following code to it:
```javascript
lastID = conn.lookup('ubm_navshortcut', 'ID', {
expression: 'code',
condition: 'equal',
values: {code: 'req_cityRegion'}
});
if (!lastID) {
console.log('\t\tcreate `City regions` shortcut');
lastID = conn.insert({
fieldList: ['ID'],
entity: 'ubm_navshortcut',
execParams: {
desktopID: desktopID,
code: 'req_cityRegion',
caption: 'City region',
iconCls: 'fa fa-braille',
displayOrder: 20,
cmdCode: JSON.stringify({
cmdType: 'showList',
cmdData: {params: [{entity: 'req_cityRegion', method: 'select', fieldList: '*'}]}
}, null, '\t')
}
});
}
lastID = conn.lookup('ubm_navshortcut', 'ID', {
expression: 'code',
condition: 'equal',
values: { code: 'req_cityRegion' }
})
if (!lastID) {
console.log('\t\tcreate `City regions` shortcut')
lastID = conn.insert({
fieldList: ['ID'],
entity: 'ubm_navshortcut',
execParams: {
desktopID: desktopID,
code: 'req_cityRegion',
caption: 'City region',
iconCls: 'fa fa-braille',
displayOrder: 20,
cmdCode: JSON.stringify({
cmdType: 'showList',
cmdData: { params: [{ entity: 'req_cityRegion', method: 'select', fieldList: '*' }] }
}, null, '\t')
}
})
} else {
console.info('\t\tuse existed shortcut with code `req_cityRegion`', lastID)
}
```
Run the generateDDL and initialize scripts or init.cmd.
Run generateDDL and initialize commands or `init` script from [Getting Started](courses/tutorial-v5/cityPortalTutorials-v5/gettingStarted.md) part of this tutorial.
Add a new field to the `req_reqList-fm.def` form
And the last step - add a new field to the `req_reqList-fm.def`:
```text
{ attributeName: "regionList" /*Regions name*/}
```
![reqListForm](img/usingMany/reqListForm.png)
This field has the multiple select type.
Now, we can make the multiple select for City Regions list:
![reqListForm](img/usingMany/reqListForm.png)
## SELECT to the Many attribute
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment