From 787b31c72c58ab9c10421861b373c751e2a555df Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 28 Jul 2016 14:41:58 -0700 Subject: [PATCH] xml/templates/dao.tpl - Simplify fieldKeys() The information here is redundant with the information in `fields()`. We can make `fields()` more hookable (and reduce SLOC) by generating this. QA note: To test that these patches had no functional impact, I used some helper scripts: https://gist.github.com/totten/4473975264fc2a788391d3c5ef5bf18b Fun fact: This is net-reduction of ~2,500 SLOC. --- xml/templates/dao.tpl | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index 0d432b382a..7b5a9af9cc 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -61,13 +61,6 @@ class {$table.className} extends CRM_Core_DAO {ldelim} */ static $_fields = null; - /** - * static instance to hold the keys used in $_fields for each field. - * - * @var array - */ - static $_fieldKeys = null; - /** * static instance to hold the FK relationships * @@ -220,26 +213,16 @@ class {$table.className} extends CRM_Core_DAO {ldelim} {rdelim} /** - * Returns an array containing, for each field, the arary key used for that - * field in self::$_fields. + * Return a mapping from field-name to the corresponding key (as used in fields()). * * @return array + * Array(string $name => string $uniqueName). */ static function &fieldKeys( ) {ldelim} - if ( ! ( self::$_fieldKeys ) ) {ldelim} - self::$_fieldKeys = array ( -{foreach from=$table.fields item=field} - '{$field.name}' => -{if $field.uniqueName} - '{$field.uniqueName}' -{else} - '{$field.name}' -{/if}, - -{/foreach} {* table.fields *} - ); - {rdelim} - return self::$_fieldKeys; + if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim} + Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields())); + {rdelim} + return Civi::$statics[__CLASS__]['fieldKeys']; {rdelim} /** -- 2.25.1