Topic on Project:Support desk

Jump to navigation Jump to search

Getting Extension:Cargo to return row _ID as part of template

8
FrugalTPH (talkcontribs)

I'm trying to implement an Action tracking system using templates, but can't for the life of me find out how to get Cargo to return the unique row _ID in the template associated with that record.

For example, my intended use-case is as follows:

Typing {{Action|Description=A task needs doing}} on any page (even multiple times per article) should create a new row in the actions table and display the following where the template was applied:

ActionID: 1

Description: A task needs doing.


Then on other pages, I'd like to be able recall / reference a task by typing {{Action|Id=1}} which would output the same summary as above, i.e.:

ActionID: 1

Description: A task needs doing.


I've tried all manner of ways of implementing this using extension IDProvider & extension variables to juggle about unique ID numbers, but I can't help feeling there must be a simpler way using Cargo's own _ID field. Cargo must be keeping track of this template usage under the bonnet anyhow, so I just want a way of being able to read that _ID value & display it in the template output.

Yaron Koren (talkcontribs)

Well, the _ID field is a global field, for all calls to a template across all pages - so instead of 1, 2, 3, the values for _ID for a single page might be 2, 10, 76. (More likely, though, the values would be something like 10, 11, 12.) Is that the issue you ran into? Or is it something else?

FrugalTPH (talkcontribs)

Well, for example, I've got two cargo tables:

  • Nonconformities
  • Actions

Each has common columns _ID, _pageName, _pageTitle, _pageNamespace, _pageID.

From what I can tell, _ID is just an the primary key for each of those tables. An incremental row ID that is applied when records get raised. They don't appear to be global to me.

I'd like to be able to include for example Actions._ID in the template output, so that I can use it as the basis of a unique Action ID number for later referencing.

FrugalTPH (talkcontribs)

For example:

<noinclude> <pre> {{Action |Description= }} </pre> {{#cargo_declare:_table=Actions|Description=String}} </noinclude><includeonly> {{#cargo_store:_table=Actions|Description={{{Description|}}}}} '''Description:''' {{{Description|}}} '''Action ID:''' {{{_ID|}}} </includeonly>


Hmm, the formatting of the above isn't coming out very well.

FrugalTPH (talkcontribs)
Yaron Koren (talkcontribs)

Hi - right, _ID is global per table. Okay, now I understand the issue. That #cargo_store stuff won't work at all, because what you're trying to do is not get data from the template into Cargo, but rather get data from Cargo into the template. So you need a call like "{{#cargo_query:_table=Actions|fields=_ID|where=_pageName='{{PAGENAME}}' AND Description='{{{Description|}}}' }}". (That's just to display the ID in the original template - the call to display the data elsewhere would need a different query.)

Another option is to use the NumerAlpha extension - use the #counter parser function from there in order to both display a (non-global) ID and store it in Cargo. The query to display that action elsewhere would then need to be passed in both a page name and the ID.

FrugalTPH (talkcontribs)

@Yaron Koren

This has solved my problem now so thanks for that. Also thanks for developing & making Cargo available, its a fantastic extension! :)

In the end I've made two templates. 1st is a <nowiki>{{NewAction|Title=}}</nowiki> which does the Cargo_Declare and Cargo_Store part of creating a new Action. 2nd part is a <nowiki>{{ExistingAction|ID=}}</nowiki> which does Cargo_Queries to retrieve record values by ID. The NewAction template, at its end, actually calls the ExistingAction template itself, to straight away display a summary table of the action, showing the unique ID of that Action for future reference.

As Yaron noted, these tables are global across the mw site, effectively giving me an auto-incrementing global action ID that I can use to track Actions (which is exactly what I was after in the first place - don't even need to use Extension:IDprovider now, Cargo does it for me!). :)

Yaron Koren (talkcontribs)

Great!

Reply to "Getting Extension:Cargo to return row _ID as part of template"