/** * Saves $parent with section if need * * @param Page $parent * * @return Page */ private function insureParentIsSection(Page $parent) { if (!$parent->hasMainSection()) { $this->em->getRepository('BackBee\\CoreDomain\\NestedNode\\Page')->saveWithSection($parent); $this->em->flush($parent); } return $parent; }
/** * Returns the maximum position of children of $page. * * @param Page $page * * @return integer */ private function getMaxPosition(Page $page) { if (false === $page->hasMainSection()) { return 0; } $query = $this->createQueryBuilder('p'); $max = $query->select($query->expr()->max('p._position'))->andParentIs($page)->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR); return null === $max ? 0 : $max; }
/** * Sets the parent node. * * @param Page $parent * * @return Page */ public function setParent(Page $parent) { if (!$parent->hasMainSection()) { throw new InvalidArgumentException('A parent page must be a section'); } if (!$this->hasMainSection() || $this->isRoot()) { $this->setSection($parent->getSection()); } else { $this->getSection()->setParent($parent->getSection()); } return $this; }
/** * Add query part to select a specific subbranch of tree. * * @param Page|null $page The parent page to check or null to select roots * * @return PageQueryBuilder */ public function andParentIs(Page $page = null) { if (null === $page) { // Looking for root return $this->andWhere($this->getSectionAlias() . '._parent IS NULL')->andWhere($this->getSectionAlias() . ' = ' . $this->getAlias()); } if (false === $page->hasMainSection()) { // Page is leaf, no child return $this->andWhere('1 = 0'); } $suffix = $this->getSuffix(); $qOr = $this->expr()->orX(); $qOr->add($this->getSectionAlias() . '._parent = :parent' . $suffix)->add($this->getAlias() . '._section = :parent' . $suffix); return $this->andWhere($qOr)->andWhere($this->getAlias() . '._level = :level' . $suffix)->setParameter('parent' . $suffix, $page->getSection())->setParameter('level' . $suffix, $page->getLevel() + 1); }