這是一個延續this的問題。Grails select將不會返回正確的數據
我有一個Address
類,它包含基本的街道地址信息。我也有一個User
類,它具有physicalAddress
,mailingAddress
,cargoDestinations
和cargoSources
的屬性。該User
類看起來是這樣的:
class User {
String username
String password
String firstName
String lastName
String businessName
String phoneNumber
Address physicalAddress
Address mailingAddress
static hasMany = [accounts:Account, cargoSources:Address, cargoDestinations:Address, cargoes:Cargo, loadsLogged:Load, loadsDelivered:Load]
Set accounts, cargoSources, cargoDestinations, cargoes
static mappedBy = [loadsLogged:"loggedBy", loadsDelivered:"deliveredBy"]
//some other stuff after this
而且Address
類看起來是這樣的:
class Address {
static belongsTo = [user:User]
String streetAddress
String city
String state
String zip
BigDecimal taxRate
//some other stuff after this
我跟着教程here大部分。在第5步我的模板看起來是這樣的:
<g:select
from="${account.user.cargoDestinations}"
name="cargoDestinations" value="">
</g:select>
的問題是,不是隻返回cargoDestinations
,模板返回與該用戶相關聯的所有地址。如果我將from="${account.user.cargoDestinations}"
更改爲from="${account.user.physicalAddress}"
或from="${account.user.mailingAddress}"
,我會得到預期結果,因此我知道我的問題與cargoDestinations
變量的映射方式有關。我怎樣才能解決這個問題,而不用更改我的課程文件太多?
有幾個問題:1)「用戶」和「地址」關係是雙向的嗎? 2)如果是這樣,你的用戶域中是否有['mappedBy'](http://www.grails.org/doc/latest/ref/Domain%20Classes/mappedBy.html)? ......不確定是否會導致這種情況,但這是需要考慮的事情。 –
@Rob我更新了我的問題,提供了關於我的課程的更多詳細信息。 – ubiquibacon