PartKeepr\CategoryBundle\Services\CategoryService::getRootNode PHP Method

getRootNode() public method

Returns the root node for a tree.
public getRootNode ( ) : AbstractCategory
return PartKeepr\CategoryBundle\Entity\AbstractCategory
    public function getRootNode()
    {
        $rootNodes = $this->entityRepository->getRootNodes();
        if (count($rootNodes) == 0) {
            throw new RootNodeNotFoundException();
        }
        $rootNode = reset($rootNodes);
        return $rootNode;
    }

Usage Example

 protected function createFootprint($footprintName, $footprintData)
 {
     /**
      * @var FootprintCategory
      */
     $footprintCategoryRootNode = $this->footprintCategoryService->getRootNode();
     $footprint = new Footprint();
     $footprint->setName($footprintName);
     if (array_key_exists('description', $footprintData)) {
         $footprint->setDescription($footprintData['description']);
     }
     if (array_key_exists('category', $footprintData)) {
         $footprintCategory = $this->addFootprintCategoryPath(explode('/', $footprintData['category']), $footprintCategoryRootNode);
         $footprint->setCategory($footprintCategory);
     }
     if (array_key_exists('image', $footprintData)) {
         $footprintImage = new FootprintImage();
         $file = $this->kernel->locateResource(self::FOOTPRINT_PATH . $footprintData['image']);
         $this->uploadedFileService->replaceFromFilesystem($footprintImage, new File($file));
         $footprint->setImage($footprintImage);
     }
     $this->entityManager->persist($footprint);
 }