public function create($params) { $model = new $this->model(); $nsm = new Manager(new Config($this->entityManager, $this->model)); foreach ($params as $k => $v) { $f = 'set' . ucfirst($k); if (method_exists($model, $f)) { $model->{$f}($v); } } if ($model->getParentId() > 0) { // add new post as a child of another post $parent = $nsm->fetchTree($model->getParentId()); $parent->addChild($model); return $model; } else { $nsm->createRoot($model); return $model; } }
public function fetchTree() { return $this->nestedSetManager->fetchTree(); }
private function getSubcategoriesDQL($rootCategoryId = 'all', $countryId = null) { $em = $this->getEntityManager(); $config = new Config($em, 'Evrika\\MainBundle\\Entity\\Category'); $nsManager = new Manager($config); if ($rootCategoryId == 'all') { $rootNode = $nsManager->fetchTree(Category::FEED_CATEGORIES_ROOT_ID); $selectedCategories = $rootNode->getDescendants(); } else { $rootNode = $em->find('EvrikaMainBundle:Category', $rootCategoryId); $rootNode = $nsManager->wrapNode($rootNode); $selectedCategories = $rootNode->getDescendants(); $selectedCategories[] = $rootNode; } $regionalCategoriesIds = array(); $commonCategoriesIds = array(); foreach ($selectedCategories as $category) { if ($category->getNode()->isRegional()) { $regionalCategoriesIds[] = $category->getNode()->getId(); } else { $commonCategoriesIds[] = $category->getNode()->getId(); } $categoriesIds[] = $category->getNode()->getId(); } $regionalCategoriesDQL = ''; $commonCategoriesDQL = ''; if (!empty($regionalCategoriesIds)) { $regionalCategoriesDQL = '(c.id IN (' . implode(',', $regionalCategoriesIds) . ')' . ($countryId ? ' AND p.country = ' . $countryId : '') . ')'; } if (!empty($commonCategoriesIds)) { $commonCategoriesDQL = 'c.id IN (' . implode(',', $commonCategoriesIds) . ')'; } $query = ' AND (' . $regionalCategoriesDQL . ($commonCategoriesDQL && $regionalCategoriesDQL ? ' OR ' : '') . $commonCategoriesDQL . ' OR (c.id IN (' . implode(',', $categoriesIds) . ') AND p.country IS NULL))'; return $query; }