2011-12-27 84 views
0

我需要爲IT項目中的用戶管理開發應用程序。這是爲了學習Grails而完成的。我有一些問題開始:在這種情況下,我如何確定我的域模型?

在我的示例應用程序,用戶有很多任務,屬於一個項目,有很多假期狀態,屬於資源規劃和多數民衆贊成它。 (種類的要求!)

現在.....當涉及到領域建模,我如何實際建模?我想出了這樣的建模解決方案:

class User { 
     //relationships. . . . 
     static belongsTo = [ company : Company, role : Role, resource : Resource] 
     static hasMany = [ holidays : Holiday, tasks : Tasks ] 
     Profile profile 
     Project project 
     String login 
     String password 
     static constraints = { 
       login(unique:true,size:6..15) 
       profile(nullable:true) 
     } 
     String toString() { 
       this.login 
     } 
} 

現在利用Grails腳手架。我產生了這個觀點,嗯,那就是我碰到的地方!

有了這個模型,顯然當創建一個新的用戶時,我需要放棄項目細節,資源細節。即使我可以根據需要更改視圖,例如我只需要兩個字段,即新用戶的登錄名和密碼以註冊我的網站。但根據數據建模,我如何處理其他領域,如配置文件,項目和其他關係。這聽起來很複雜!我知道我是網絡開發的新手,我想要你的建議,我該如何着手解決這個問題?

在此先感謝。

+1

嘗試http://grails.org/doc/2.0.x/ref/Domain%20Classes/hasOne.html配置文件和項目和腳手架所有的域類。 – 2011-12-27 02:10:35

回答

3

您需要覆蓋控制器中的save操作並在其中填充這些附加字段。

嘗試在UserController中加入以下代碼:

def save = { 
    def userInstance = User.get(params.id) 
    if (!userInstance) { 
     userInstance = new User() 

     // here you can fill in additional fields: 
     userInstance.profile = myMethodToGetTheProfile() 
     userInstance.project = myMethodToGetTheProject() 
     ... 
    } 
    userInstance.properties = params 
    if (userInstance.save(flush: true)) { 
     flash.message = message(code: 'default.created.message', args: [message(code: 'User.propertyName.label', default: 'User'), userInstance.id])} 
     redirect(action: session.lastActionName ?: "list", controller: session.lastControllerName ?: controllerName) 
    } else { 
     render(view: "form", model: [userInstance: userInstance, mode: 'edit']) 
    } 
} 

實施方法獲取默認的項目取決於您的應用程序的邏輯。

+0

+1好:)以及這是我做的。我在需要的類上創建了靜態腳手架。在特定的'_form.gsp'中,我通過html和gsp字段在相應的類中添加了相應的表單域。例如,在創建'User'時,我將'Profile'字段放在同一個表單中(因爲這兩個類之間存在關係,使用'profile.name'等語法)。它完美的作品。現在我的問題是,我做了什麼是一個好習慣?還是一個壞的? – 2011-12-27 11:19:26

+1

我認爲這是一個很好的做法。不過,請記住驗證傳遞的參數,因爲用戶可以欺騙寫入表單域的值(例如,可以訪問其他項目) – socha23 2011-12-27 12:54:30

+0

感謝您的回答。 – 2011-12-27 15:18:35

相關問題