我是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>
我的印象是,这会考验存在。 如果类别DOES有父母,它可以正常工作,但一旦parentid = 0,它就会爆炸。
No row with the given identifier exists: [ProductCategory#0]
我试图检查== 0,但它不起作用,我假设因为'parent'应该是一个对象。
那么如何才能使它假设parentid = 0与parent = null相同,或者是否为父级?
由于
答案 0 :(得分:1)
我想我可能找到了答案:
parent column: 'parentid', ignoreNotFound: true;
ignoreNotFound在文档中没有任何地方,但似乎有效!
答案 1 :(得分:0)
parentid
不应该等于0.应该是null
。
我在你的问题中不明白,你怎么能有parentid == 0?
答案 2 :(得分:-1)
您无需手动处理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) { ... }