【问题标题】:cakephp how do you order by a count field?cakephp 你如何按计数字段排序?
【发布时间】:2013-08-23 22:52:22
【问题描述】:

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

$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')
        );

【问题讨论】:

  • 使用文档中的虚拟字段 (book.cakephp.org/2.0/en/models/virtual-fields.html),然后您也可以轻松地对它们进行排序。
  • 这只是一个报告,所以不会经常使用。如果我添加虚拟场,那么我不知道我的模型关系将如何发挥作用。
  • 我觉得virtualfields很好,但是如果它们依赖于其他模型,那真的没有任何好处。
  • 我认为您不了解虚拟字段,它们也可以在实际保存调用之前按需添加。这将使它们暂时附着,因此不会弄乱任何东西。每当使用非表字段时,虚拟字段都是要走的路。结果与您的结果几乎相同,但“更加cakish”。
  • 哦,好的。刚刚试运行,效果很好!!!谢谢!

标签: php sql cakephp orm sql-order-by


【解决方案1】:

应该去MODEL并添加

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

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

【讨论】:

    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多