2015-08-14 63 views
-1

我有我的工作的應用程序之間的循環連接,我有一個有點問題如何防止實體

的應用程序基本上是這樣的: Client Group (1) - Call client (2) (parent_id: 1, path: 1/2/) - Do something (3) (parent_id: 1, path: 1/3/) - Another group (4) (parent_id: 1, path: 1/4/) -- Another task (5) (parent_id: 4, path: 1/4/5/) Call supplier and order client products (8) (parent_id: 7, path: 6/7/8/, dependency_id: 2, dependency_path: 2/) Handle products group (6) (dependency_id: 8, dependency_path: 2/8/) - Some task (7) (parent_id: 6, path 6/7/)

您可以創建組/遞歸文件夾結構中的任務。我已經解決了父/子與parent_path列之間的循環連接問題。

每個任務/組可以依賴於另一個實體,然後才能將其標記爲已完成。假設您有一項任務「呼叫客戶端」,並且您有另一項任務「呼叫供應商和訂購客戶端產品」。在「呼叫客戶端」被標記爲完成之前,您不能將「呼叫供應商」標記爲已完成。

問題是,我無法弄清楚如何確保一個實體只能依賴於另一個實體,而這個實體並不處於任何連接狀態。

這意味着,如果fooEntity取決於barEntity,你不能讓barEntity依賴fooEntity(或者說是依賴於fooEntity任何實體,或者說是fooEntity的孩子任何單位或者????)

我解決了與dependency_path專欄直接受撫養人的問題,但其餘的超出了我。

因此,大家可以看到我真的很困惑,這和沒有我的想法應該是什麼規則,所以在這裏,一切都會順利進行和最終用戶將不能夠把事情搞得一團糟。

你的幫助真的會真的很感激!謝謝:)

回答

1

如果您可以在PHP上進行檢查,而不是僅限於MySQL,「路徑」是解決問題的關鍵。

每個「路徑」的最後一位數對此沒有必要,但其餘的可以像過濾器一樣。

// Get each digit of "path" but its last 
function get_parent_digits($path){ 
    $digits = explode('/', substr($path, 0, -1)); 
    array_pop($digits); 
    return $digits; 
} 

// Compare two and check if one can be dependant on another 
function can_be_dependant($parent_digits_1, $parent_digits_2){ 
    // Brother 
    if(count($parent_digits_1) === count($parent_digits_2)){ 
     return true; 
    } 
    // One is not another's child nor decendant 
    else{ 
     return $parent_digits_1 === array_diff($parent_digits_1, $parent_digits_2); 
    } 
} 

然後,你可以檢查它像...:

// $row_1 and $row_2 are the results of mysql select query 
$digits_1 = get_parent_digits($row_1['path']); 
$digits_2 = get_parent_digits($row_2['path']); 
if(can_be_dependant($digits_1, $digits_2)){ 
    // Okay 
}else{ 
    // No 
} 

$ row_1或$ row_2可能是新的數據,你可以將其保存在數據庫中之前與功能驗證。

+0

謝謝你,我最終這樣做:http://pastie.org/private/losywgshez82nc5hn933a。問題是,我仍然可以創建一個實體,該實體是依賴於我要爲其設置依賴項的實體的實體的後代。 (這裏是一個樹的例子:http://pastie.org/private/oeaymcbplt1rjzyjplzj0w) – sUP

+0

確認你的目標是什麼。 實體1取決於實體3. 實體1.1​​是實體1的孩子,這意味着實體1.1​​也取決於實體3? 實體3不能依賴實體1.1​​嗎? 取而代之,您寫了「Call supplier ...」(8)取決於「Call client」,儘管「Handle products group」(6)是(8)的根。 這是矛盾的,你要警告的時候(8)會發生什麼? –

+0

最後我想我解決了它。解決方案在這裏:http://pastie.org/private/cniomypxn38xrolq8b6bw – sUP