Grails - 检查项目是否有父级

发布于 2024-08-19 23:20:26 字数 1123 浏览 8 评论 0原文

我是 Grails、Groovy 和 GSP 的新手。

我有一个域类“ProductCategory”。

class ProductCategory {

    static constraints = {
    }

    static mapping = {
        table 'product_category';
        version false;
        cache usage: 'read-only';
        columns {
            parent column: 'parentid';
            procedure column: 'procid';
        }

    }

    static hasMany = [children:ProductCategory];

    ProductProcedure procedure;
    Integer lineorder;
    String name;
    ProductCategory parent;
    String templatelink;
    char offline;

    String toString() {
        return id + " (" + name + ")";
    }
}

每个类别可以有一个父类别。我正在使用现有的数据库,并且该表有一列“parentid”来执行此操作。当一个类别没有父级(根级别)时,其 Parentid 为 0。

我有一个 GSP 试图显示有关父级的数据(如果有)。

<g:if test="${category.parent}">
hello
</g:if>

我的印象是这将测试是否存在。 如果该类别确实有父类别,它就可以正常工作,但一旦parentid = 0,它就会崩溃。

No row with the given identifier exists: [ProductCategory#0]

我尝试检查 ==0,但它不起作用,我认为因为“parent”应该是一个对象。

那么我怎样才能让它假设parentid = 0与parent = null相同,或者没有父项?

谢谢

I'm new to Grails, Groovy and GSP.

I have a domain class "ProductCategory".

class ProductCategory {

    static constraints = {
    }

    static mapping = {
        table 'product_category';
        version false;
        cache usage: 'read-only';
        columns {
            parent column: 'parentid';
            procedure column: 'procid';
        }

    }

    static hasMany = [children:ProductCategory];

    ProductProcedure procedure;
    Integer lineorder;
    String name;
    ProductCategory parent;
    String templatelink;
    char offline;

    String toString() {
        return id + " (" + name + ")";
    }
}

Each category CAN have a parent. I am using an existing database, and the table has a column 'parentid' to do that. When a category has no parent (root level), its parentid is 0.

I have a GSP trying to show data about the parent if any.

<g:if test="${category.parent}">
hello
</g:if>

I was under the impression that this would test for existence.
It works fine if the category DOES have a parent, but as soon as parentid=0, it blows up.

No row with the given identifier exists: [ProductCategory#0]

I tried to check for ==0, but it didn't work, I assume because 'parent' is supposed to be an object.

So how can I make it so that it assumes that parentid=0 is the same as parent=null, or NO parent?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-08-26 23:20:26

我想我可能已经找到了答案:

parent column: 'parentid', ignoreNotFound: true;

ignoreNotFound在文档中没有任何地方,但它似乎有效!

I think I may have found the answer:

parent column: 'parentid', ignoreNotFound: true;

ignoreNotFound is nowhere on the documentation, but it seems to work!

热鲨 2024-08-26 23:20:26

parentid 不应等于 0。它应为 null

我在你的问题中不明白的是,你怎么能有parentid == 0?

parentid should not be equal to 0. It should be null.

What I don't understand in your question, is how can you have parentid == 0 ?

瀟灑尐姊 2024-08-26 23:20:26

您不需要手动处理parentid。一旦您定义了这样的域类:

Class Foo {
    Bar bar
}

Gorm/Grails 就会自动为您创建一个外键列。如果您定义属性 nullable:

Class Foo {
     Bar bar
     static constraints = {
         bar(nullable:true)
     }
}

...您可以将其设置为 null 并测试 null:

def f = new Foo(bar:null)
if (f.bar == null) { ... }

You don't need to handle the parentid manually. As soon as you define a domain class like this:

Class Foo {
    Bar bar
}

Gorm/Grails will automatically create a foreign key column for you. And if you define the property nullable:

Class Foo {
     Bar bar
     static constraints = {
         bar(nullable:true)
     }
}

...you can just set it to null and test for null:

def f = new Foo(bar:null)
if (f.bar == null) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文