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

moveTo() public méthode

Move to another parent CategoryItem
public moveTo ( CategoryItem $item, CategoryItem $parent = null ) : CategoryItem
$item Xpressengine\Category\Models\CategoryItem item object
$parent Xpressengine\Category\Models\CategoryItem new parent item object
Résultat Xpressengine\Category\Models\CategoryItem
    public function moveTo(CategoryItem $item, CategoryItem $parent = null)
    {
        $oldParent = $item->getParent();
        if ($parent !== null) {
            if ($item->getKey() === $parent->getKey()) {
                throw new UnableMoveToSelfException();
            }
            if ($oldParent !== null && $oldParent->getKey() == $parent->getKey()) {
                return;
            }
        }
        if ($oldParent !== null) {
            $this->unlinkHierarchy($item, $oldParent);
            $item->{$item->getParentIdName()} = null;
        }
        if ($parent !== null) {
            $this->linkHierarchy($item, $parent);
            $item->{$item->getParentIdName()} = $parent->getKey();
        }
        $item->save();
        // 연관 객체 정보들이 변경 되었으므로 객채를 갱신 함
        return $item->newQuery()->find($item->getKey());
    }

Usage Example

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