CakePHP 3.x - 如何在查询中使用sum

时间:2016-05-24 18:22:14

标签: cakephp cakephp-3.x

我正在尝试获取我附加的查询的agregation结果。我已经挣扎了几天,我似乎没有找到解决方案。帮助将不胜感激

这些是模型:

class EventsPromotersTable extends Table
{
    public function initialize(array $config)
    {
        $this->addAssociations([
            'belongsTo' => ['Events', 'Promoters'],
            'hasMany' => ['Payments'] 
        ]);
    }
}

class EventsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addAssociations([
            'hasMany'=> ['TicketTypes'],
            'belongsToMany' => ['EventsPromoters' => ['through' => 'EventsPromoters']]
        ]);
    }
}

class PromotersTable extends Table
{
    public function initialize(array $config)
    {
        $this->addAssociations([
            'belongsTo' => ['MultimediaFiles'],
            'belongsToMany' => ['EventsPromoters' => ['through' => 'EventsPromoters']]
        ]);
    }
}

class PaymentsTable extends Table
{
    public function initialize(array $config)
    {
        $this->addAssociations([
            'belongsTo' => ['Promoters'],
            'hasMany' => ['Sales']
        ]);         
    }
}

class SalesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addAssociations([
            'belongsTo' => ['TicketTypes', 'Payments']
        ]); 
    }
}

class TicketTypesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addAssociations([
            'belongsTo' => ['Events'],
            'hasMany' => ['Sales']
        ]); 
    }
}

我正在尝试检索TicketTypes销售的sum(Sales.quantity)Promoters)金额。

这是我的查找方法:

$query= $this->EventsPromoters
    ->find()
    ->contain([
        'Promoters' => function($q) {
            return $q
                ->select(['Promoters.id', 'Promoters.name'])
            ;
        },
        'Payments.Sales.TicketTypes' => function($q) {
            return $q
                ->select(['TicketTypes.id', 'TicketTypes.name'])
            ;
        }
    ])
    ->innerJoinWith('Events', function ($q) use($event_slug) {
            return $q->where(['Events.slug' => $event_slug]);
    })
;

find方法正常运行。现在我必须添加聚合,但我不知道如何。

我的期望是这样的:

  'promoter' => object(Cake\ORM\Entity) {
                'id' => (int) 101,
                'name' => 'Lucas Moura',
                  'ticket_types' => [
                        (int) 0 => object(Cake\ORM\Entity) {
                            'id' => (int) 101,
                            'name' => 'Primera Preventa',
                            'sales' => 3 <<<<<< AGGREGATION
                            '[new]' => false,
                            '[accessible]' => [
                                '*' => true
                            ],
                            '[dirty]' => [],
                            '[original]' => [],
                            '[virtual]' => [],
                            '[errors]' => [],
                            '[repository]' => 'TicketTypes'
                        }
                        (int) 1 => object(Cake\ORM\Entity) {
                            'id' => (int) 102,
                            'name' => 'Palco VIP',
                            'sales' => 9 <<<<<< AGGREGATION
                            '[new]' => false,
                            '[accessible]' => [
                                '*' => true
                            ],
                            '[dirty]' => [],
                            '[original]' => [],
                            '[virtual]' => [],
                            '[errors]' => [],
                            '[repository]' => 'TicketTypes'
                        }

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我的解决方案是创建虚拟字段,我不知道是否是最好的方式,但对我有用。祝你好运。