cakephp3.x-如何在find查询中使用sum

cakephp3.x-如何在find查询中使用sum,cakephp,cakephp-3.x,Cakephp,Cakephp 3.x,我正在尝试获取所附加查询的否定结果。我已经挣扎了好几天,但似乎没有找到解决办法。我们将不胜感激 这些是模型: class EventsPromotersTable extends Table { public function initialize(array $config) { $this->addAssociations([ 'belongsTo' => ['Events', 'Promoters'],

我正在尝试获取所附加查询的否定结果。我已经挣扎了好几天,但似乎没有找到解决办法。我们将不胜感激

这些是模型:

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']
        ]); 
    }
}
我正在尝试检索
促销员出售的
票款类型的数量(
sum(Sales.quantity)

这是我的查找方法:

$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'
                        }
'promotor'=>对象(蛋糕\ORM\Entity){
'id'=>(int)101,
'name'=>'Lucas Moura',
“票证类型”=>[
(int)0=>对象(Cake\ORM\Entity){
'id'=>(int)101,
'name'=>'Primera Preventa',
“销售额”=>3[],
“[虚拟]”=>[],
“[错误]”=>[],
“[repository]”=>“TicketTypes”
}
(int)1=>对象(Cake\ORM\Entity){
'id'=>(int)102,
'name'=>'Palco VIP',
“销售额”=>9[],
“[虚拟]”=>[],
“[错误]”=>[],
“[repository]”=>“TicketTypes”
}

有什么想法吗?谢谢!

我也遇到了同样的问题,我的解决方案是创建虚拟字段,我不知道这是否是最好的方法,但对我有效。祝你好运。

我遇到了同样的问题,我的解决方案是创建虚拟字段,我不知道这是否是最好的方法,但对我有效。祝你好运。

你读过这篇文章吗?实际的技术是什么您面临的所有问题?“我不知道如何”留下了很大的解释空间。您好@ndm。我不知道如何添加聚合(总和)。请看我期望的代码。我想检索促销员出售的票证类型的数量,没有关于付款和销售的信息,只是数量。您尝试过使用吗?这里有相同的问题,有人知道如何实现吗?稍后将尝试虚拟字段@BogdanKuštan并让您知道。@BogdanKuštan which模型你认为我应该使用虚拟场吗?推广人,对吗?你读过吗?你面临的实际技术问题是什么?“我不知道如何”留下了很大的解释空间。嗨@ndm。我不知道如何添加聚合(总和)。请看我期望的代码。我想检索促销员出售的票证类型的数量,没有关于付款和销售的信息,只是数量。您尝试过使用吗?这里有相同的问题,有人知道如何实现吗?稍后将尝试虚拟字段@BogdanKuštan并让您知道。@BogdanKuštan whic你认为我应该使用虚拟场地吗?推广员,对吗?