Xpressengine\Category\CategoryHandler::removeItem PHP Méthode

removeItem() public méthode

Remove single item or all descendant
public removeItem ( CategoryItem $item, boolean $force = true ) : boolean
$item Xpressengine\Category\Models\CategoryItem item object
$force boolean if true then remove all descendant
Résultat boolean
    public function removeItem(CategoryItem $item, $force = true)
    {
        $count = 1;
        /** @var CategoryItem $desc */
        foreach ($item->descendants as $desc) {
            if ($force === true) {
                $desc->ancestors()->detach();
                $desc->descendants()->detach();
                $desc->ancestors()->newPivotStatement()->where($desc->getDescendantName(), $desc->getKey())->where($desc->getDepthName(), 0)->delete();
                $desc->delete();
                $count++;
            } else {
                // 하위 아이템을 삭제하지 않는 경우
                // 하위 아이템을 삭제할 대상 아이템의 부모 아이템의
                // 자식 아이템으로 변경해 줌
                $desc->descendants()->newPivotStatement()->where($desc->getDescendantName(), $desc->getKey())->where($desc->getAncestorName(), '!=', $item->getKey())->where($desc->getDepthName(), '>', 0)->decrement($desc->getDepthName());
                $parentId = ($parent = $item->getParent()) ? $parent->getKey() : null;
                $desc->{$desc->getParentIdName()} = $parentId;
                $desc->save();
            }
        }
        $result = $item->delete();
        // 아이템이 삭제되면 아이템이 속해있던 카테고리 그룹의 아이템 수량을 감소 시킴
        $item->category->decrement('count', $count);
        return $result;
    }

Usage Example

 public function destroyItem(CategoryHandler $handler, $categoryId)
 {
     $id = Input::get('id');
     if ($id === null || !($item = $handler->getItem($id))) {
         throw new InvalidArgumentHttpException();
     }
     DB::beginTransaction();
     try {
         $handler->removeItem($item);
     } catch (Exception $e) {
         DB::rollBack();
         throw $e;
     }
     DB::commit();
 }
All Usage Examples Of Xpressengine\Category\CategoryHandler::removeItem