Last active
November 2, 2015 14:17
-
-
Save damianoporta/9dcbeb153aadb5c9bcdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Recupero numero totale degli annunci di ogni settore. | |
* Di default vengono restituiti i dati della tabella tags ed un campo aggiuntivo | |
* chiamato `total_ads` che indica il numero totale degli annunci associati a | |
* quel singolo tag settoriale. | |
* | |
* Opzioni utilizzabili: | |
* | |
* - tagGroupId: (default: 1) E' il gruppo di associazione con la tabella Ads | |
* - scope: (default: 1) E' lo scope che identifica tutti i tag settoriali | |
* nella tabella `tags`. | |
* - tagsList: (opzionale) Qualora non si volessero recuperare tutti i tags | |
* regionali grazie a questa opzione è possible passare una lista di ids tags | |
* da filtrare. | |
* | |
* @param Query $query | |
* @param array $options | |
* @return Query | |
*/ | |
public function findTotalAdsPerSector(Query $query, array $options) | |
{ | |
$defaultOptions = [ | |
'tagGroupId' => 1, | |
'scope' => 1 | |
]; | |
$options = array_merge($defaultOptions, $options); | |
$query | |
->select(['Tags.id', 'Tags.tag_name', 'Tags.slug', 'total_ads' => $query->func()->count('Ads.id')]) | |
->join([ | |
'SectorsTags' => [ | |
'table' => 'tags_associations', | |
'type' => 'INNER', | |
'conditions' => [ | |
'SectorsTags.foreign_key = Ads.id', | |
'SectorsTags.model' => 'Ads', | |
'SectorsTags.tag_group_id' => $options['tagGroupId'] | |
], | |
], | |
'Tags' => [ | |
'table' => 'tags', | |
'type' => 'RIGHT', | |
'conditions' => 'SectorsTags.tag_id = Tags.id', | |
] | |
]) | |
->where(['Tags.scope' => $options['scope']]); | |
// Controllo se è stata passata una lista specifica di tags | |
if (isset($options['tagsList'])) { | |
$query->where(['Tags.id' => $options['tagsList']], ['Tags.id' => 'integer[]']); | |
} | |
$query->group('Tags.id'); | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment