我正在尝试获取我附加的查询的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']
        ]); 
    }
}

我正在尝试检索Promoters销售的TicketTypessum(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'
                        }

有任何想法吗? 谢谢!

===============>>#1 票数:0

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

  ask by dfc translate from so

未解决问题?本站智能推荐: