Merge pull request #1205 from totten/master-backbone-findcreate
[civicrm-core.git] / xml / templates / dao.tpl
index 029f0d7620750706d39cda65486245c9d6c66222..b5211939b7596602e54e2909e684078f8f104a5d 100644 (file)
@@ -64,6 +64,14 @@ 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
+      */
+      static $_fieldKeys = null;
+
      /**
       * static instance to hold the FK relationships
       *
@@ -123,22 +131,27 @@ class {$table.className} extends CRM_Core_DAO {ldelim}
         parent::__construct( );
     {rdelim}
 
-{if $table.foreignKey}
+{if $table.foreignKey || $table.dynamicForeignKey}
     /**
-     * return foreign links
+     * return foreign keys and entity references
      *
+     * @static
      * @access public
-     * @return array
+     * @return array of CRM_Core_EntityReference
      */
-    function links( ) {ldelim}
-  if ( ! ( self::$_links ) ) {ldelim}
-       self::$_links = array(
+    static function getReferenceColumns() {ldelim}
+      if (!self::$_links) {ldelim}
+        self::$_links = array(
 {foreach from=$table.foreignKey item=foreign}
-                                   '{$foreign.name}' => '{$foreign.table}:{$foreign.key}',
+          new CRM_Core_EntityReference(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}'),
 {/foreach}
-                             );
-        {rdelim}
-        return self::$_links;
+
+{foreach from=$table.dynamicForeignKey item=foreign}
+          new CRM_Core_EntityReference(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}'),
+{/foreach}
+        );
+      {rdelim}
+      return self::$_links;
     {rdelim}
 {/if} {* table.foreignKey *}
 
@@ -222,6 +235,30 @@ class {$table.className} extends CRM_Core_DAO {ldelim}
           return self::$_fields;
       {rdelim}
 
+      /**
+       * Returns an array containing, for each field, the arary key used for that
+       * field in self::$_fields.
+       *
+       * @access public
+       * @return array
+       */
+      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;
+      {rdelim}
+
       /**
        * returns the names of this table
        *