From 28e5cfbde499b7d08649596d0f675ad8bba0143a Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 22 Jul 2018 11:27:20 +0200 Subject: bake: sort tables by default (with config) + allow ORDER BY hooks --- app-from-scratch.sh | 6 ++++ .../CustomTheme/src/Template/Bake/Model/table.twig | 37 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/app-from-scratch.sh b/app-from-scratch.sh index 5d35c2b..fba441e 100644 --- a/app-from-scratch.sh +++ b/app-from-scratch.sh @@ -369,3 +369,9 @@ git add plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php git add config/bake_extra.php git add ../app-from-scratch.sh git commit -m "bake: fix Search filter support (bad stringifyList())" +# b49f4d5ccd5a61e901c448ad94495750bb2d0269 + +# bake: sort tables by default (with config) + allow ORDER BY hooks +editor plugins/CustomTheme/src/Template/Bake/Model/table.twig +git add ../app-from-scratch.sh plugins/CustomTheme/src/Template/Bake/Model/table.twig +git commit -m "bake: sort tables by default (with config) + allow ORDER BY hooks" diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig index fb3a417..788e48f 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig @@ -39,6 +39,9 @@ namespace {{ namespace }}\Model\Table; {% set uses = ['use Cake\\ORM\\Query;', 'use Cake\\ORM\\RulesChecker;', 'use Cake\\ORM\\Table;', 'use Cake\\Validation\\Validator;'] %} +{%- if BakeExtra.hasOrderOpts(name) or BakeExtra.hasOrderHooks(name) %} +{% set uses = uses|merge(['use ArrayObject;', 'use Cake\\Event\\Event;']) %} +{% endif %} {{ uses|join('\n')|raw }} {{ DocBlock.classDescription(name, 'Model', annotations)|raw }} @@ -174,4 +177,38 @@ class {{ name }}Table extends Table return $searchManager; } {% endif %} + +{%- if BakeExtra.hasOrderOpts(name) or BakeExtra.hasOrderHooks(name) %} + + /** + * @return \Cake\ORM\Query + */ + public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) + { +{%- if BakeExtra.hasOrderOpts(name) %} + + $order = $query->clause('order'); + if ($order === null || !count($order)) { + $query->order([ + {{- Bake.stringifyList(BakeExtra.getOrderOpts(name), {'indent': 4})|raw -}} + ]); + } +{% endif %} +{%- if BakeExtra.hasOrderHooks(name) %} + + $orderHooks = [ + {{- Bake.stringifyList(BakeExtra.getOrderHooks(name), {'indent': 3})|raw -}} + ]; + $order = $query->clause('order'); + if ($order !== null && count($order)) { + $order->iterateParts(function ($rhs, &$lhs) use ($orderHooks) { + if ( array_key_exists($lhs, $orderHooks) ) { + $lhs = $orderHooks[$lhs]; + } + return $rhs; + }); + } +{% endif %} + } +{% endif %} } -- cgit v1.1