2013-08-02 118 views
3

我正在使用CakePHP一段時間,並遇到了一個問題,我需要檢查模型是否與其他模型相關聯。有沒有內置的功能?檢查一個模型是否與cakephp中的另一個模型相關聯?

BLOG 
-- hasMany COMMENTS 
-- hasOne Profile 

COMMENTS 
-- belongsTo BLOG 

我正在尋找像一些功能:

hasAssociation('blog', 'comments'); //should return true 
hasAssociation('blog', 'whatever'); //should return false 
hasAssociation('profile', 'blog'); //should return false 
hasAssociation('blog', 'profile'); //should return true 

如果沒有這樣的功能,我怎麼能寫我自己?我不符合邏輯。請建議。

+0

在什麼情況下,你將有可能會或可能不存在的關係模型? –

+0

我使用長輪詢爲我的應用程序實現了一個通知系統。 'NotificationsController'處理所有的通知部分。它的工作很棒。但爲了顯示通知詳細信息,我需要使用NotificationsController返回的ID從數據庫中獲取詳細信息。這些ID可能代表不同的部門(有8個),我需要在提取結果之前檢查關聯。其種類很難解釋。希望這是有道理的。 – Konsole

+0

當然,在你的例子中,通知和部門之間始終存在關係。是否有數據存在是另外一個問題。 –

回答

相關問題