user4408933
user4408933

Reputation:

keystoneJS relationship to self

I want to create a Category model that can hold another category, but having a problem with reference field that I can set my current category to it self

Any suggestions how to achieve hierarchical categories? Does KeystoneJS have filter like 'not equal'? In other hand, maybe I can set default reference field to it self and it will be like a root...

My current code below:


    var keystone = require('keystone'),
        Types = keystone.Field.Types;

    var PageCategory = keystone.List('PageCategory', {
        map: { name: 'name' },
        autokey : { from: 'name', path: 'key'}
    });

    PageCategory.add({
        name: { type: String, required: true, unique: true},
        image: { type: Types.CloudinaryImage, label: "Category Image"},
        description : { type: Types.Html, wysiwyg: true},
        parent: { type: Types.Relationship, ref: "PageCategory", label: "Parent category"}
    });

    PageCategory.relationship({ ref: "PageCategory", path: "parent"});

    PageCategory.register();

Upvotes: 5

Views: 1023

Answers (1)

doug
doug

Reputation: 127

I think you have misunderstood how Model.relationship() works.

It has three options:

  • path, this is the "virtual" field name that will hold the values
  • ref, this is the model that we reference
  • refPath, this is the field in the referenced model that we populate path with

I think something in line with this will work for you

PageCategory.relationship({ ref: "PageCategory", path: "children", refPath: "parent"});

Upvotes: 0

Related Questions