2016-05-24 109 views
1

我試圖獲得我附加的查詢的聚合結果。我一直在掙扎幾天,我似乎找不到解決方案。幫助將不勝感激CakePHP 3.x-如何在查找查詢中使用總和

這些機型:

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'] 
     ]); 
    } 
} 

我試圖檢索TicketTypessum(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' 
         } 

任何想法?謝謝!

+0

你讀過[**關於使用SQL函數**的文檔](http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions)嗎?你面臨的實際技術問題是什麼? 「我不知道如何」爲解釋留下了很多空間。 – ndm

+0

嗨@ndm。我不知道如何添加聚合(總和)。請看我期望的代碼。我想檢索發起人銷售的票類型的金額,而沒有關於付款和銷售的信息,只是金額。 – dfc

+0

您嘗試過使用[Virtual fields](http://book.cakephp.org/3.0/en/orm/entities.html#creating-virtual-fields)嗎? –

回答

0

我有同樣的問題,我的解決方案是創建虛擬領域,我不知道是否是最好的方式,但爲我工作。祝你好運。