CRM-13938, added table info
authorkurund <kurund@civicrm.org>
Thu, 12 Dec 2013 23:41:34 +0000 (15:41 -0800)
committerkurund <kurund@civicrm.org>
Sat, 15 Feb 2014 01:05:38 +0000 (17:05 -0800)
----------------------------------------
* CRM-13938: Create Annotations for entity files
  http://issues.civicrm.org/jira/browse/CRM-13938

CRM/Core/CodeGen/EntitySpecification.php
xml/templates/entity.tpl

index f4d194a0a63c2be4359dbbc1ad4947b7a61bfedc..6a701e5c25cf9591d14a6bbcd5072f9889df08d2 100644 (file)
@@ -262,6 +262,43 @@ class CRM_Core_CodeGen_EntitySpecification {
       $table['dynamicForeignKey'] = $dynamicForeign;
     }
 
+    // create tags for indexes
+    $table['indexes'] = '';
+    $table['uniqueConstraints'] = '';
+    $indexElements = array();
+    if (!empty($table['index'])) {
+      $uniqueElements = array();
+      foreach($table['index'] as $value) {
+        $indexElements[] = '@ORM\Index(name="' . $value['name'] . '", columns={"' . implode(',', $value['field']) . '"})';
+        if (isset($value['unique'])) {
+          $uniqueElements[] = '@ORM\UniqueConstraint(name="' . $value['name'] . '", columns={"' . implode(',', $value['field']) . '"})';
+        }
+      }
+    }
+
+    // note that we explicitly create indexes for all foreign keys
+    if (!empty($table['foreignKey'])) {
+      foreach($table['foreignKey'] as $value) {
+        $indexElements[] = '@ORM\Index(name="' . $value['uniqName'] . '", columns={"' . $value['name'] . '"})';
+      }
+    }
+
+    //build table annotation
+    $tableInfo = '@ORM\Table(name="' . $name . '"';
+    // add unique constraints if any
+    if (!empty($uniqueElements)) {
+      $tableInfo .= ', uniqueConstraints={' . implode(',', $uniqueElements) . '}';
+    }
+
+    // add indexes if any
+    if (!empty($uniqueElements)) {
+      $tableInfo .= ', indexes={' . implode(',', $indexElements) . '}';
+    }
+
+    $tableInfo .= ')';
+
+    $table['tableInfo'] = $tableInfo;
+
     $tables[$name] = &$table;
     return;
   }
index 6035a06422c56a1e644ac8fc3b433805374f21ce..74b97c4c52852142fcd24570d24c1d30cb306160 100644 (file)
@@ -41,7 +41,7 @@ use Doctrine\ORM\Mapping as ORM;
 /**
  * {$table.className}
  *
- * @ORM\Table(name="{$table.name}", {literal}uniqueConstraints={@ORM\UniqueConstraint(name="UI_name", columns={"name"})}{/literal})
+ * {$table.tableInfo}
  * @ORM\Entity
  */
 class {$table.className} extends \Civi\Core\Entity {ldelim}