Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Clean the menu table from deleted groups
- */
- private function cleanMenuFromDeletedGroup()
- {
- /**
- * Find menus which have custom(groups) visible_by level
- */
- $customMenus = Yii::app()->db->createCommand()
- ->select('*')
- ->from('menu')
- ->where('JSON_CONTAINS(visible_by, CAST(false as JSON), "$.all")')
- ->query();
- /**
- * If we have such custom menus loop through them
- * and search for deleted group ($this->idst)
- */
- if ( $customMenus && is_array($customMenus)){
- // Hold groups ids here answer will be [menuId => groupId]
- $groupsToDelete = array();
- foreach ( $customMenus as $menu){
- $groups = json_decode($menu['visible_by'])->groups;
- $groupIndex = array_search($this->idst, $groups);
- if ( $groupIndex !== false){
- $groupsToDelete[$menu['id']] = $groupIndex;
- }
- }
- }
- // If we have such groups delete just the id from menu table
- if ( !empty($groupsToDelete)){
- foreach ( $groupsToDelete as $id => $index){
- /**
- * $id - menuId
- * $index - position in the JSON field "visible_by->groups"
- */
- Yii::app()->db
- ->createCommand('UPDATE menu SET visible_by = JSON_REMOVE(visible_by, "$.groups['.$index.']") WHERE menu.id = '.$id)->query();
- }
- }
- }
Add Comment
Please, Sign In to add comment