【问题标题】:Doctrine querybuilder where element is child of a doctrine nested set tree parent学说查询构建器,其中元素是学说嵌套集树父级的子级
【发布时间】:2015-03-21 10:20:15
【问题描述】:

我想构建一个查询构建器,它返回在其父级中的某个特定点具有给定元素的元素。

在父级上加倍或使用 root 是不行的,因为可能有多个级别。

到目前为止,我有这个代码:

return = $this
        ->createQueryBuilder('o')
        ->leftJoin('o.organizationCategories', 'c')
        ->leftJoin('c.parent', 'parent')
        ->where('parent = ?2')
        ->orWhere('parent.parent = ?2')
        ->setParameter(2, $parent)
        ->getQuery()
        ->getResult();

当然,如果父母不是根父母,祖父母不是父母,这将不再起作用。

我该怎么办?

【问题讨论】:

    标签: symfony doctrine-orm tree doctrine nested-sets


    【解决方案1】:

    我最终做了以下事情

    /**
     * ProductRepository
     *
     * This class was generated by the Doctrine ORM. Add your own custom
     * repository methods below.
     */
    abstract class HasCategoryRepository extends EntityRepository
    {
    
        /**
         * @return QueryBuilder
         */
        abstract function getExplorerQueryBuilder();
    
        /**
         * Filters products on ingredients, which belong to the category 'consommable'
         *
         * @param ObjectCategory $objectCategory
         * @throws \Exception
         * @return \Doctrine\ORM\QueryBuilder
         */
        public function findByTopCategoryQueryBuilder($objectCategory)
        {
            $objectCategoryRepository = $this->_em->getRepository('AppBundle:Core\ObjectCategory');
    
            if (!$objectCategory instanceof ObjectCategory) $objectCategory=$objectCategoryRepository->findOneBy(array('slug' => $objectCategory));
            if (!$objectCategory) throw new \Exception("La catégorie n'a pas été trouvée.");
    
            $queryBuilder = $this->getExplorerQueryBuilder();
    
            $categories = array_merge(array($objectCategory), $objectCategoryRepository->children($objectCategory));
            $categoriesIds = array_map(function(BaseCategoryClass $category){return $category->getid();}, $categories);
    
            $queryBuilder = $queryBuilder
                    ->andWhere($this->createQueryBuilder('object')->expr()->in('c.id', $categoriesIds))
                ;
    
            return $queryBuilder;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      相关资源
      最近更新 更多