ICT:Automatic IDs
{{#set:Description=Documentation for generating and querying automatic IDs using Page Forms and Cargo.}}
Automatic ID Generation for Cargo Tables
This page documents the workflow for generating automatic, composite IDs using the Page Forms and Cargo extensions.
Overview
While Page Forms does not have a "native" ID generator button, unique IDs can be constructed by:
- Combining user-input attributes in the storage template.
- Using the form's page name formula to ensure uniqueness.
- Enforcing uniqueness via form validation.
1. Template Logic (The "Store" Method)
To create an ID from a combination of attributes, logic is placed inside the `{{#cargo_store}}` function.
Example Cargo Declaration
Define your table in your Schema template:
{{#cargo_declare:
_table = Projects
|ProjectID = String
|Category = String
|Year = Integer
}}
Example Cargo Store
In your data template, concatenate the fields to create the `ProjectID`:
<includeonly>
{{#cargo_store:
_table = Projects
|ProjectID = {{{Category|}}}-{{{Year|}}}-{{#time:U}} <!-- Category-Year-UnixTimestamp -->
|Category = {{{Category|}}}
|Year = {{{Year|}}}
}}
</includeonly>
2. Form Definition
To ensure the user doesn't create a duplicate ID, use the `unique` and `page name` parameters in Page Forms.
Page Name Formula
In the `{{{info}}}` tag, define how the page is named automatically:
{{{info|page name=<Projects[Category]>-<Projects[Year]>-<unique number;start=001>}}}
Field Validation
Force the form to check if an ID already exists:
{{{field|ProjectID|unique|input type=text|hidden}}}
3. Querying Records
To retrieve data using your custom ID, use the `{{#cargo_query}}` function as documented on MediaWiki.org.
Filter by Composite ID
{{#cargo_query:
|tables = Projects
|fields = _pageName=Page, Category, Year
|where = ProjectID = 'Hardware-2024-171523421'
|format = table
}}
Filter using Wildcards
To find all projects from a specific category using the ID prefix:
{{#cargo_query:
|tables = Projects
|fields = ProjectID, Year
|where = ProjectID LIKE 'Hardware-%'
|format = ul
}}
Extensions Used
- Page Forms: Handles the user interface and page naming.
- Cargo: Handles data storage and querying.
- ParserFunctions: Used for string manipulation and date formatting.