[ColdBox 3.6.0] Getting external key on orm relationship

I have an Coldfusion orm application that have two object:

Widget.cfc

// Primary Key
property name="id" fieldtype="id" column="" generator="native";

// Properties
property name="col" type="string";
property name="created_at" type="date";
property name="kind" type="string";
property name="name" type="string";
property name="label" type="string";
property name="range" type="string";
property name="row" type="numeric";
property name="size" type="numeric";
property name="size_x" type="numeric";
property name="size_y" type="numeric";
property name="source" type="string" default="null";
property name="targets" type="numeric";
property name="update_interval" ormtype="int";
property name="update_at" type="date";

//Relationship
property name="dashboards" fieldtype="many-to-many" 
        linktable="DashboardWidgets" 
        type="array" cfc="dashboard" 
        cascade="all-delete-orphan" inverse="true" singularname="dashboard";

And dashboard.cfc

// Primary Key
property name="id" fieldtype="id" column="id" generator="native";

// Properties
property name="name" type="string";
property name="time" type="timestamp";
property name="layout" type="string";
property name="created_at" type="timestamp";
property name="updated_at" type="timestamp";
property name="locked" type="boolean";

//Relationships 
property name="widgets" fieldtype="many-to-many" 
    linktable="DashboardWidgets" type="array" cfc="widget" 
    cascade="all-delete-orphan" singularname="widget";

I need to output as json all the property of widget.cfc and also the dashboard id that is defined in the relationship.

So what I have is something like:

[{"id":5,"kind":"number",
"name":"dsfasdfa",
"size_x":1,
"size_y":1,
"source":"demo",
"update_interval":36000,
"dashboards":[{"id":3,"name":"Undefined name",
    "time":"July, 24 2013 20:52:44 +0200",
    "layout":"test",
    "created_at":"July, 24 2013 20:52:44 +0200",
    "updated_at":"July, 24 2013 20:52:44 +0200",
    "locked":false,
    "widgets":null}]
}]

What I need insted is something like:

[{"id":5,
"kind":"number",
"name":"dsfasdfa",
"size_x":1,
"size_y":1,
"source":"demo",
"update_interval":36000,
"dashboard_id":3
}]

How can I achieve this? I need only the id of the dashboard.

First, how are you creating the JSON that you currently have? Is this just a straight serialization of the Widget object?

Second, since “dashboards” is a many-to-many property of widget, it seems that you’ll actually potentially have multiple dashboards-per-widget. So then, depending on the requirements of your code, you may want to return an array of dashboard id’s, rather than just one.

Either way, default CF serialization probably won’t get you where you want to go. What I’ve done for similar requirements in my apps is to create a custom transformer that I can run my entities through to have more control on how the desired output is generated.

You might also check out approaches such as what is done in methods like getMemento() in ContentBox entities: https://github.com/Ortus-Solutions/ContentBox/blob/master/modules/contentbox/model/content/BaseContent.cfc.

I use a simple rest call:

function show(event,rc,prc){
rc = event.getCollection();

local.dashboard = dashboardService.get(rc.dashboard_id);
local.json = local.dashboard.getWidgets();
event.renderData(data=local.json, type=“json”);
}

I have tried adding manually to json a property:

function show(event,rc,prc){
rc = event.getCollection();

local.dashboard = dashboardService.get(rc.dashboard_id);
local.json = local.dashboard.getWidgets();
local.json.dashboard_id = rc.dashboard_id;
event.renderData(data=local.json, type=“json”);
}

but I get an error: cannot cast [DASHBOARD_ID] string to a number value

Try this instead

local.json[dashboard_id] = rc.dashboard_id;

I believe that will work rather than

local.json.dashboard_id = rc.dashboard_id;

Hi Andrew,

I’ve tried:

cannot cast [DASHBOARD_ID] string to a number value

Got same error. I’m on railo, could this be the problem?