Skip to content

Instantly share code, notes, and snippets.

@ADmad
Created August 20, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ADmad/26b79787a59892c34f5f to your computer and use it in GitHub Desktop.
Save ADmad/26b79787a59892c34f5f to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Behavior;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\Utility\Hash;
class MetaBehavior extends Behavior
{
public function findMeta(Query $query, array $options)
{
$query
->contain([
'Meta' => function ($q) {
return $q->hydrate(false);
}
])
->formatResults(function ($results) use ($options) {
$results = $results->map(function ($value, $key) {
if (!empty($value['meta'])) {
$value['meta'] = Hash::combine($value['meta'], '{n}.key', '{n}.value');
}
return $value;
});
return $results;
});
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment