0

我尝试了以下方法,但无法对其进行排序。

$options = array(
                         'limit'  => $limit,
                         'offset' => $this->Account->get_offset($page, $limit),
                         'fields' => array(
                                           'Accounts.login', 
                                           'Accounts.name', 
                                           'Accounts.created', 
                                           'COUNT(DISTINCT(Buyer.id)) AS "Accounts__buyer_count"', 
                                           'COUNT(DISTINCT(Seller.id)) AS "Accounts__seller_count"', 
                                           'COUNT(DISTINCT(Certificate.id)) AS "Accounts__certificate_count"'
                                    ),
                         'joins' => array(
                                          array(
                                                'table'      => 'buyers',
                                                'alias'      => 'Buyer',
                                                'type'       => 'LEFT',
                                                'conditions' => array('Buyer.account_id = Accounts.id')
                                          ),
                                          array(
                                                'table'      => 'sellers',
                                                'alias'      => 'Seller',
                                                'type'       => 'LEFT',
                                                'conditions' => array('Seller.buyer_id = Buyer.id')
                                          ),
                                          array(
                                                'table'      => 'certificates',
                                                'alias'      => 'Certificate',
                                                'type'       => 'LEFT',
                                                'conditions' => array('Certificate.buyer_id = Buyer.id')
                                          )
                                    ),
                         'group' => array('Accounts.id'),
                         'order' => array('Accounts__certificate_count' => 'ASC')
        );
4

1 回答 1

0

应该去模型并添加

var $virtualFields = array(
    'buyer_count'       => 'COUNT(DISTINCT(Buyer.id))',
    'seller_count'      => 'COUNT(DISTINCT(Seller.id))',
    'certificate_count' => 'COUNT(DISTINCT(Certificate.id))',
);

之后,您可以对表中相同的其他字段进行排序

于 2014-02-24T07:59:00.813 回答