Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
g:在grails中选择Savetodatabaseselecteditem_Grails - Fatal编程技术网

g:在grails中选择Savetodatabaseselecteditem

g:在grails中选择Savetodatabaseselecteditem,grails,Grails,我有表:Products和Shop。(我使用grailsgenerateall生成控制器)Shop有很多产品 我试图做的是列出所有的商店,并保存到数据库中选定的商店时,创建一个新的产品。 我使用 <g:form controller="product" action="save" role="form"> <div class="form-horizontal" role="form"> <div class="form-group">

我有表:Products和Shop。(我使用grailsgenerateall生成控制器)Shop有很多产品 我试图做的是列出所有的商店,并保存到数据库中选定的商店时,创建一个新的产品。 我使用

<g:form controller="product" action="save" role="form">
    <div class="form-horizontal" role="form">
        <div class="form-group">
            <label class="col-lg-3 control-label">Product Name:</label>
            <div class="col-lg-8">
                <g:textField name="productName" class="form-control" value="${product.productName}"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Product Barcode</label>
            <div class="col-lg-8">
                <g:textField name="date expired" class="form-control" value="${product.productBarcode}"></g:textField>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Product Shop</label>
            <g:select class="selectpicker" name="products.id" from="${tr.com.netiket.lkkstoreapp.Shop.list()}" value="shop?.products.id" optionValue="shopName"></g:select>
        </div>
</g:form>

产品名称:
产品条码
产品店
当我点击“创建”按钮时,它会显示

类[class tr.com.nur.storeapp.Product]的属性[shop]不能为空


这一点看起来不对劲:

名称应为店铺id,值应为产品的店铺id(如有):


此位看起来不正确:

名称应为店铺id,值应为产品的店铺id(如有):

这是我的保存方法。实际上我没有在这里写任何东西。生成域


这是我的保存方法。实际上我没有在这里写任何东西。生成域。

谢谢@Gregg的回答。但这一次它说;NOTNULL属性引用瞬态值-必须先保存瞬态实例,然后才能执行当前操作您是否有任何想法您必须显示您的保存方法。我想知道这是否有关联,或者你是否已经发现了另一个问题来解决这个问题?我作为一个回答发帖感谢@Gregg的回答。但这一次它说;NOTNULL属性引用瞬态值-必须先保存瞬态实例,然后才能执行当前操作您是否有任何想法您必须显示您的保存方法。我想知道它是否相关,或者你是否已经发现了另一个问题来解决这个问题?我作为一个答案发布了我已经改变了你的答案,并尽可能地简化了它-基本上相同的东西太多了,但有很多被注释掉的打印-你看到了真正发现你在代码中的位置,同时开发你应该尝试放置一些在if语句等中测试println语句,以确保一切正常,并且作为旁注,您所处的位置正确。虽然如此,用户回答自己的问题时没有问题。目前这并不是真正的答案。因此,删除它并编辑实际问题并将上述内容添加到其中可能是值得的。我希望你已经解决了你的问题。这篇评论是关于未来用户的良好实践,他们可能会提到这个问题,你自己的声誉也会受到负面影响。我已经改变了你的答案,并尽可能地简化了它——基本上有太多相同的东西,但有很多被注释掉的println——你可以在开发代码时真正发现你在代码中的位置你应该试着在你的if语句中加入一些testprintln语句,以确保一切正常,并且作为旁注,你所处的位置是正确的。虽然如此,用户回答自己的问题时没有问题。目前这并不是真正的答案。因此,删除它并编辑实际问题并将上述内容添加到其中可能是值得的。我希望你已经解决了你的问题。这篇评论是关于未来用户的良好实践,他们可能会提到这个问题以及你自己的声誉,因为你得到了负面分数。我解决了我的问题。我在这里写信是为了帮助别人。我所做的就是将optionkey添加到g:select other way shop return null。我解决了我的问题。我在这里写信是为了帮助别人。我所做的就是将optionkey添加到g:select other way shop return null。
 @Transactional
def save(Shop shop) {
    //println "in shop save"
    def currentUser=(User)springSecurityService.currentUser
    shop.user=currentUser
    shop.validate()
    if (!shop) {
        //println "I have no shop"
        transactionStatus.setRollbackOnly()
        notFound()
        return
    }

    //if (shop.hasErrors()) {
     if (shop.save(flush:true)) {
       //println "shop has errors"
        transactionStatus.setRollbackOnly()
        respond shop.errors, view:'create'
        shop.errors.allErrors
        return
    }

    //shop.save flush:true
    //println "shop has saved"

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'shop.label', default: 'Shop'), shop.id])
             //println "redirecting"
            redirect shop
        }
        '*' { respond shop, [status: CREATED] }
    }
}