Grails - 检查项目是否有父级
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我可能已经找到了答案:
ignoreNotFound在文档中没有任何地方,但它似乎有效!
I think I may have found the answer:
ignoreNotFound is nowhere on the documentation, but it seems to work!
parentid
不应等于 0。它应为null
。我在你的问题中不明白的是,你怎么能有parentid == 0?
parentid
should not be equal to 0. It should benull
.What I don't understand in your question, is how can you have parentid == 0 ?
您不需要手动处理parentid。一旦您定义了这样的域类:
Gorm/Grails 就会自动为您创建一个外键列。如果您定义属性 nullable:
...您可以将其设置为 null 并测试 null:
You don't need to handle the parentid manually. As soon as you define a domain class like this:
Gorm/Grails will automatically create a foreign key column for you. And if you define the property nullable:
...you can just set it to null and test for null: