5

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();

atish shimpi
  • 4,873
  • 2
  • 32
  • 50

1 Answers1

0

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"});
doug
  • 127
  • 1
  • 8