簡體   English   中英

使用MySQL的學說,我可以選擇父類別和子類別中的所有產品嗎?

[英]Doctrine with MySQL, Can I select all products within parent and sub categories?

我已經做了一些尋找答案的工作,但是大多數結果還是不夠清晰,或者我發現很難以當前的模式來實現...我希望實現的是查詢從產品中選擇所有產品表與類別表中的類別ID相匹配,但是現在我希望也獲得屬於所述父類別的子類別的產品。 我正在使用Codeigniter使用Doctrine 2,到目前為止我的功能看起來像這樣

function searchForProducts( $offset, $limit ) {
    $search = $this->input->get('search');
    $category = (int)$this->input->get('category');
    $supplier = (int)$this->input->get('supplier');

    for( $i = 0; $i < 2; $i++ ) {
        $select = ($i == 0) ? 'count(p)' : 'p';
        $qb = $this->em->createQueryBuilder();
        $qb ->select($select)
            ->from(self::ENTITY, 'p');

        if( $i != 0) {
            $qb ->setFirstResult( (int)$offset )
                ->setMaxResults( (int)$limit );
        }

        if( $search ) {
            $qb ->where( "p.title LIKE :search" )
                ->orWhere( "p.sku LIKE :search" )
                ->setParameter('search', "%$search%");
        }

        if( $category ) {
            $qb ->andWhere( "p.category = ?1" )
                ->setParameter(1, $category);
        }

        if( $supplier ) {
            $qb ->andWhere( "p.supplier = ?2" )
                ->setParameter(2, $supplier);
        }

        if( $i == 0 ) {
            $this->totalRows =  $qb->getQuery()->getSingleScalarResult();
        } else {
            return $qb->getQuery()->getResult();
        }
    }   
}

我也不認為先獲得所有產品然后從應用程序級別進行操作是不切實際的,因為我正在使用分頁,並且產品可能會變得很大。

當您執行Product -> Category -> Child Categories -> Product ,結果Product不會在同一列中返回。 因此,教義將在同一個庄園中為它們補水。 如果您想在所有產品上使用分頁,那就不好了。]

最好的選擇是將本機SQL與結果集映射一起使用。 然后,您的查詢應使用UNION,以便主Product與子Product的列在同一列中

為了回答我自己的問題,我決定采取簡單的方法,考慮到我最多只能容納40個類別,所以我認為這不是什么大問題。

在類別模型類中,我創建了2個靜態方法,

static function getCategoryWithChildren( $id = NULL ) {
    $obj = new self;
    $parent = $obj->getCategory( $id );
    $list = array();
    self::iterateCategoriesChildren($parent, $list);
    return array_reverse($list);    
}

static function iterateCategoriesChildren( $category, &$array ) {
    if( $category->hasChildren() ) {
        foreach( $category->getChildren() as $child ) {
            self::iterateCategoriesChildren($child, $array);                
        }
    }
    $array[] = $category;
}

因此,這幾乎將獲得父對象並迭代所有子對象,並將每個1分配給平面數組。

然后從我的searchProducts方法中,我在$ category檢查下添加了更多內容。

        if( $category ) {
            if( $this->settings->get('product_child_categories') ) {
                $categories = Categories_model::getCategoryWithChildren($category);
                foreach($categories as $_category) {
                    $qb ->orWhere( "p.category = " . $_category->getID() );
                }                   
            } else {                
                $qb ->andWhere( "p.category = ?1" )
                    ->setParameter(1, $category);
            }               
        }

可能不是最好的或不切實際的,但它可以正常工作並滿足我目前的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM