统计学说中的外键数量

时间:2012-09-11 15:30:33

标签: doctrine dql

这是对此问题的跟进:count number of foreign keys

我正在使用doctrine 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的各种方法,但似乎没有任何结果。知道我该怎么做我想要的东西?

1 个答案:

答案 0 :(得分: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