simov / express-admin

MySQL, MariaDB, PostgreSQL, SQLite admin for Node.js
MIT License
1.17k stars 223 forks source link

OneToMany mapping with two tables #103

Open aniruddhasm opened 8 years ago

aniruddhasm commented 8 years ago

Hello team,

I've a Category masters table with column (name, parent) Topic table with columns (name, category_id) SubTopic table with columns (name, topic_id)

The way I want to show topic_id in Listing UI is name-category(Actual category name) Below is my code

 "name": "topic_id",
                "verbose": "Topic",
                "control": {
                    "select": true
                },
                "oneToMany": {
                    "table": "topic",
                    "pk": "id",
                    "columns": [
                        "name",
                        "category_id"
                    ]
                },
                "type": "int(100)",
                "allowNull": false,
                "defaultValue": null,
                "listview": {
                    "show": true
                },
                "editview": {
                    "show": true
                }

The above code produces output as

(name)cartoon 1(category_id)

where cartoon is name 1 is the category_id Here instead of 1 I want the actual category name.

Please help.

simov commented 8 years ago

Currently it's not possible to configure such relationship through the configuration.

aniruddhasm commented 8 years ago

ok Thank You

aniruddhasm commented 8 years ago

Hello,

I'm trying a work around

I've created a field helpertext in topic table that is hidden. So when I save the topic table I have written a preSave event to save the helpertext as (name)cartoon 1(category_id)


exports.preSave = function (req, res, args, next) {
    if (args.debug) console.log('preSave');
    debugger;
    if (args.name == 'topic') {
        record = args.data.view.topic.records[0].columns;
        record.helpertext = record.name + "-" + record.category_id;
        console.log(JSON.stringify(record));
    }
    next();
}

but instead of this I want category name. So I did this

exports.preSave = function (req, res, args, next) {
    if (args.debug) console.log('preSave');
    debugger;
    if (args.name == 'topic') {
        record = args.data.view.topic.records[0].columns;
                var inline = args.data.manyToOne.topic;

        console.log(JSON.stringify(args.data));
        record.helpertext = record.category_id + "-" + record.name;
        console.log(JSON.stringify(record));
    }
    next();
}

I am getting error at var inline = args.data.manyToOne.topic;

Please help

ikrasnykh commented 7 years ago

+1. Would love to see this feature