CakePHP按相关表的计数搜索

时间:2012-03-01 12:31:21

标签: cakephp relationship

我需要进行搜索,按相关表格的“COUNT”进行过滤,但我不能这样做..

以下是查询:

            $this->paginate = array
            (
             'Establishment' => array
             (
              'conditions' => array
              (
               'EstablishmentsCategory.slug' => $category,
              ),
              'limit'      => 10,
              'order'      => $order,
              'fields'     => array('Establishment.*'),
              'contain'    => array
              (
               'EstablishmentsCategory',
               'EstablishmentsImage',
               'EstablishmentsCoupon' => array('fields' => array('id'))
              )
             )
            );

        $data = $this->paginate('Establishment');

现在这个返回数组如下:

    [0] => Array
    (
        [Establishment] => Array
            (
                [id] => 2
                [establishments_category_id] => 1
                [slug] => xxxxxx
                [name] => xxxxxx
                [description] => xxxxxx
                [active] => 0
                [modified] => 2012-01-25 15:31:23
                [created] => 2011-07-29 10:03:51
            )

        [EstablishmentsCategory] => Array
            (
                [id] => 1
            )

        [EstablishmentsCoupon] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [establishment_id] => 2
                    )

            )

        [EstablishmentsImage] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [establishment_id] => 2
                        [image] => fsdfsdf.jpg
                        [created] => 2012-01-10 11:34:06
                    )

                [1] => Array
                    (
                        [id] => 3
                        [establishment_id] => 2
                        [image] => werwerwer.jpg
                        [created] => 2012-01-10 11:34:06
                    )

                [2] => Array
                    (
                        [id] => 4
                        [establishment_id] => 2
                        [image] => werwerwer.jpg
                        [created] => 2012-01-10 11:34:06
                    )

                [3] => Array
                    (
                        [id] => 7
                        [establishment_id] => 2
                        [image] => werwerer2.jpg
                        [created] => 2012-01-10 13:31:01
                    )

            )

    )

问题:我需要找到所有“机构”,有一个或多个“机构奖励”。我没有找到解决方案:S

2 个答案:

答案 0 :(得分:1)

您应该可以使用带有group语句的having子句来执行此操作。

但是,请注意,如果您的查询需要递归设置,将查询拆分为多个查询以获取EstablishmentCoupons等,则可能无效。

$this->paginate = array(
    'Establishment' => array(
        'conditions' => array(
            'EstablishmentsCategory.slug' => $category,
        ),
        'limit' => 10,
        'order' => $order,
        'fields' => array('Establishment.*, count(EstablishmentsCoupon.id) as ec_count'),
        'contain' => array(
            'EstablishmentsCategory',
            'EstablishmentsImage',
            'EstablishmentsCoupon' => array(
                'fields' => array('id')
            )
        ),
        'group' => array('Establishment.* HAVING ec_count > 0'),
    )
);

$data = $this->paginate('Establishment');

在上面的代码中,我在ec_count参数中添加了fields,然后添加了group参数,该参数使用having子句基于ec_count子句{{1}} 1}}。

如果您收到有关该组的查询语法问题,请尝试使用来自Establishment的显式字段而不是使用Establishment。*(这是一种糟糕的做事方式,因为您可能没有使用所有字段但不知道无论如何你得到了什么。)

同样,这可能不起作用,具体取决于您的查询路径,但此模式已在我所使用的其中一个系统中有效。

答案 1 :(得分:0)

如果您使用的是belongsTo关联(并且优惠券属于企业),您可以在优惠券模型上启用counterCache。启用counterCache后,Cake将跟踪每个企业拥有的优惠券数量。

然后,您可以使用简单的查找条件搜索企业:

$this->Establishment->find( 'all', 
    conditions => array('Establishment.coupon_count >' => 0 )
    );