1

这是这个问题的后续:count number of foreign keys

我正在使用学说 1.2,我想计算属于子类别的业务数量。

以下是mysql表。

1.fi_category

+----+-----------------+-----------------+
| id | name            | slug            |
+----+-----------------+-----------------+

2.fi_subcategory

+----+-----------------+-----------------+-------------+
| id | name            | slug            | category_id |
+----+-----------------+-----------------+-------------+

3.fi_business_subcategory

+----+-------------+----------------+
| id | business_id | subcategory_id |
+----+-------------+----------------+

我正在使用这个 DQL。

$q = Doctrine_Query::create()
    ->select('c.name, c.slug, sc.name, sc.slug')
    ->from('Model_Category c')
    ->leftJoin('c.Subcategory sc')
    ->leftJoin('sc.BusinessSubcategory bsc');

这给了我这样的东西。

Array
(
    [0] => Array
    (
        [id] => 1
        [name] => Entertainment & Lifestyle
        [slug] => entertainment-lifestyle
        [Subcategory] => Array
        (
            [0] => Array
            (
                [id] => 1
                [name] => Arts and Crafts
                [slug] => arts-and-crafts
            )
            [1] => Array
            (
                [id] => 2
                [name] => Family
                [slug] => family
            )
            [2] => Array
            (
                [id] => 3
                [name] => Fashion
                [slug] => fashion
            )
        )
    )
)

我正在寻找业务数量,即返回的结果应该是这样的,具体取决于它所属的业务。

Array
(
    [0] => Array
    (
        [id] => 1
        [name] => Entertainment & Lifestyle
        [slug] => entertainment-lifestyle
        [Subcategory] => Array
        (
            [0] => Array
            (
                [id] => 1
                [name] => Arts and Crafts
                [slug] => arts-and-crafts
                [business_count] => 35
            )
            [1] => Array
            (
                [id] => 2
                [name] => Family
                [slug] => family
                [business_count] => 10
            )
            [2] => Array
            (
                [id] => 3
                [name] => Fashion
                [slug] => fashion
                [business_count] => 27
            )
        )
    )
)

使用 DQL 尝试了各种方法,但似乎没有任何效果。知道我应该如何去做我想要的吗?

4

1 回答 1

1

你试过了吗:

$q = Doctrine_Query::create()
     ->select('c.name, c.slug, sc.name, sc.slug, COUNT(sc.BusinessSubcategory.id) AS business_count')
     ->from('Model_Category c')
     ->leftJoin('c.Subcategory sc')
     ->groupBy('c.id, sc.id');

HYDRATE_ARRAY

于 2012-09-12T11:11:07.907 回答