2013-01-16 84 views
1

我需要你在編寫一個查詢的幫助 - 請查看下面選擇查詢 - 選擇父權限

類別

CREATE TABLE IF NOT EXISTS `category` (
    `catid` varchar(170) NOT NULL, 
    `name` varchar(170) NOT NULL, 
    `parent_catid` varchar(170) NOT NULL, 
    `inherit_parent_perms` enum ('Y', 'N') NOT NULL, 
    KEY `idx_wss` (`catid`) 
); 
INSERT INTO `category` VALUES('1', 'Fruits', '0', 'N'); 
INSERT INTO `category` VALUES('2', 'Apples', '1', 'N'); 
INSERT INTO `category` VALUES('3', 'Furniture', '0', 'N'); 
INSERT INTO `category` VALUES('4', 'Toys', '0', 'N'); 
INSERT INTO `category` VALUES('5', 'Gifts', '4', 'Y'); 

和權限表的表結構

權限

CREATE TABLE IF NOT EXISTS `permissions` (
    `catid` varchar(170) NOT NULL, 
    `permission_object` enum ('seller', 'buyer') NOT NULL, 
    `permission_object_id` varchar(170) NOT NULL, 
    KEY `idx_wss` (`catid`) 
); 


INSERT INTO `permissions` VALUES('1', 'seller', '100'); -- Here category 1 is accessible to 'seller' object with ID 100 
INSERT INTO `permissions` VALUES('1', 'buyer', '300'); -- Here category 1 is accessible to 'buyer' object with ID 300 

INSERT INTO `permissions` VALUES('2', 'buyer', '200'); -- Here category 2 is accessible to 'buyer' object with ID 200 

INSERT INTO `permissions` VALUES('3', 'buyer', 'all'); -- Here category 3 is accessible to all 'buyer' objects 
INSERT INTO `permissions` VALUES('3', 'seller', 'all'); -- Here category 3 is accessible to all 'seller' objects 

正如你所看到的類別表是如何在權限表中引用和類別可以通過一個ID的對象類型或所有對象類型時,對象ID設置爲可以訪問「所有」

這是查詢到目前爲止,我已經得到

SELECT id, name, p. * 
FROM `catalogue` c 
LEFT JOIN permissions p ON p.cat = c.catid 
AND (
     (
      (
       p.permission_object_id = 'all' 
       OR p.permission_object_id = '100' 
      ) 
       AND p.permission_object = 'seller' 
     ) 
     OR 
     (
      (
       p.permission_object_id = 'all' 
       OR p.permission_object_id = '100' 
      ) 
       AND p.permission_object = 'buyer' 
     ) 
    ) 
WHERE catid = 3 

問題

上面的查詢類型的作品 - 如如果類別3可由買方ID 100訪問 - 它會顯示它下面的內容。如果這個類別被允許買方ID爲'all',那麼任何買方都可以看到這個類別。

但問題是當我有子類別。因此,如果類別3可以通過'all'訪問,然後我添加兩個子類別4和5(類別3下),並告訴它繼承來自父類的4和子類別5的權限,只能通過'seller'ID 200訪問 - 那麼我不明白在這裏做什麼。

  • 我是否需要標記爲繼承父權限時複製所有父權限,並插入新行(很明顯,新的類別ID).... OR ....我可以以某種方式使用相同的查詢有一些變化(我不知道該怎麼做)。

  • 對上述任何其他更改/建議將不勝感激。

+0

有什麼建議嗎? – user1421214

回答

0

這將只處理子分類的一個層次,即如果添加的子類別,類別4 & 5.

它應該讓你開始了,雖然如果我的理解是行不通的問題正確:

-- Assuming category 5 is a sub-category of category 3 and has inherit_parent_perms='Y' 

select * 
from 
(
-- select permisssions for this category 
SELECT c.catid, c.name, p.permission_object,p.permission_object_id 
FROM `category` c 
LEFT JOIN `permissions` p ON p.catid = c.catid 
where c.catid = 5 
union 
-- select permissions from parent category if it exists 
SELECT c.catid, c.name, p.permission_object,p.permission_object_id 
FROM `category` c 
inner join `category` parent on c.parent_catid = parent.catid and c.inherit_parent_perms='Y' 
LEFT JOIN `permissions` p ON p.catid = parent.catid 
WHERE c.catid = 5 
) all_perms 
WHERE (
     (
      (
       all_perms.permission_object_id = 'all' 
       OR all_perms.permission_object_id = '100' 
      ) 
       AND all_perms.permission_object = 'seller' 
     ) 
     OR 
     (
      (
       all_perms.permission_object_id = 'all' 
       OR all_perms.permission_object_id = '100' 
      ) 
       AND all_perms.permission_object = 'buyer' 
     ) 
    ); 
+0

感謝您的回答--4和5確實有子類別,但讓我檢查一下,看看它是否有效 – user1421214