xml/templates/dao.tpl - Simplify fieldKeys()
authorTim Otten <totten@civicrm.org>
Thu, 28 Jul 2016 21:41:58 +0000 (14:41 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 28 Jul 2016 22:51:56 +0000 (15:51 -0700)
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

index 0d432b382a678911c96dfa4b26d44a6f1681e532..7b5a9af9cce5695c03ea2859208f88714b393b43 100644 (file)
@@ -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}
 
       /**