Skip to content

Instantly share code, notes, and snippets.

@damianoporta
Created November 5, 2015 08:45
Show Gist options
  • Select an option

  • Save damianoporta/d10b75e07051a5342475 to your computer and use it in GitHub Desktop.

Select an option

Save damianoporta/d10b75e07051a5342475 to your computer and use it in GitHub Desktop.
<?php
public function findTags(Query $query, array $options)
{
// Se non c'è alcuna associazione restituisco la query senza procedere
if (empty($options['association'])) {
return $query;
}
if (empty($options['matching'])) {
// Contain
return $query->contain([$options['association'] => function ($q) use ($options) {
// Condizioni
if (!empty($options['query_conditions'])) {
$q->where($options['query_conditions']);
}
// Order
if (!empty($options['query_order'])) {
$q->order($options['query_order']);
}
return $q->contain('Tags');
}]);
} else {
// Matching
$associationConditions = $this->association($options['association'])->conditions();
$query
->join([
'table' => 'tags_associations',
'alias' => $options['association'],
'type' => 'INNER',
'conditions' => [
"{$options['association']}.foreign_key = Agents.id",
"{$options['association']}.model" => "Agents",
] + $associationConditions
])
->join([
'table' => 'tags',
'alias' => "{$options['association']}Tags",
'type' => 'INNER',
'conditions' => [
"{$options['association']}.tag_id = {$options['association']}Tags.id",
]
]);
// Condizioni
if (!empty($options['query_conditions'])) {
$query->where($options['query_conditions']);
}
// Order
if (!empty($options['query_order'])) {
$query->order($options['query_order']);
}
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment