Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. id
  2. object_table
  3. object_id
  4. parent_object_table
  5. parent_object_id
  6. depth
  7.  
  8. object_table = 'table_c'
  9. object_id = 15
  10. parent_object_table = 'table_b'
  11. parent_object_id = 12
  12. depth = 1
  13.  
  14. object_table = 'table_c'
  15. object_id = 15
  16. parent_object_table = 'table_a'
  17. parent_object_id = 3
  18. depth = 2
  19.  
  20. object_table = 'table_c'
  21. object_id = 15
  22. parent_object_table = 'table_c'
  23. parent_object_id = 15
  24. depth = 0
  25.  
  26. SET @table_associations = 'pa_associations';
  27.  
  28. SET @associations_create = CONCAT(
  29. 'CREATE TABLE IF NOT EXISTS ',
  30. @table_associations,
  31. ' (
  32. `ancestorName` varchar(255) CHARACTER SET utf8 NOT NULL,
  33. `ancestorId` int(11) UNSIGNED NOT NULL,
  34. `descendantName` varchar(255) CHARACTER SET utf8 NOT NULL,
  35. `descendantId` int(11) UNSIGNED NOT NULL,
  36. `depth` int(11) UNSIGNED NOT NULL,
  37. PRIMARY KEY (`ancestorName`, `ancestorId`, `descendantName`, `descendantId`)
  38. ) ',
  39. ' ENGINE=InnoDB DEFAULT CHARSET=utf8'
  40. );
  41.  
  42. PREPARE statement FROM @associations_create;
  43. EXECUTE statement;
  44.  
  45. -- Here's an example query to acquires *all* user ancestors given a permission ID of 1:
  46.  
  47. SELECT * FROM pa_accounts AS accounts
  48. JOIN pa_associations AS associations
  49. ON accounts.id = associations.ancestorId
  50. WHERE associations.descendantId = 1
  51. AND associations.descendantName = 'pa_permissions'
  52. AND associations.ancestorName = 'pa_accounts';
  53.  
  54. -- Here's an example query that inserts a relationship between the 4th role and the 10th permission:
  55.  
  56. INSERT INTO pa_associations (ancestorName, ancestorId, descendantName, descendantId, depth)
  57. SELECT
  58. ancestorName,
  59. ancestorId,
  60. 'pa_permissions',
  61. 10,
  62. depth + 1,
  63. FROM pa_associations
  64. WHERE descendantId = 4 AND descendantName = 'pa_roles'
  65. UNION ALL SELECT 'pa_permissions', 10, 'pa_permissions', 10, 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement