return $occurrences;
}
+ function getReferenceCounts() {
+ $links = self::getReferencesToTable(static::getTableName());
+
+ $counts = array();
+ foreach ($links as $refSpec) {
+ /** @var $refSpec CRM_Core_Reference_Interface */
+ $count = $refSpec->getReferenceCount($this);
+ if ($count['count'] != 0) {
+ $counts[] = $count;
+ }
+ }
+
+ return $counts;
+ }
+
/**
* List all tables which have hard foreign keys to this table.
*
$result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
return $result;
}
+
+ public function getReferenceCount($targetDao) {
+ $targetColumn = $this->getTargetKey();
+ $params = array(
+ 1 => array($targetDao->$targetColumn, 'String')
+ );
+ $sql = <<<EOS
+SELECT count(id)
+FROM {$this->getReferenceTable()}
+WHERE {$this->getReferenceKey()} = %1
+EOS;
+
+ return array(
+ 'name' => implode(':', array('sql', $this->getReferenceTable(), $this->getReferenceKey())),
+ 'type' => get_class($this),
+ 'table' => $this->getReferenceTable(),
+ 'key' => $this->getReferenceKey(),
+ 'count' => CRM_Core_DAO::singleValueQuery($sql, $params)
+ );
+ }
}
$result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
return $result;
}
+
+ public function getReferenceCount($targetDao) {
+ $targetColumn = $this->getTargetKey();
+ $params = array(
+ 1 => array($targetDao->$targetColumn, 'String'),
+
+ // If anyone complains about $targetDao::getTableName(), then could use
+ // "{get_class($targetDao)}::getTableName();"
+ 2 => array($targetDao::getTableName(), 'String'),
+ );
+
+ $sql = <<<EOS
+SELECT count(id)
+FROM {$this->getReferenceTable()}
+WHERE {$this->getReferenceKey()} = %1
+AND {$this->getTypeColumn()} = %2
+EOS;
+
+ return array(
+ 'name' => implode(':', array('sql', $this->getReferenceTable(), $this->getReferenceKey())),
+ 'type' => get_class($this),
+ 'table' => $this->getReferenceTable(),
+ 'key' => $this->getReferenceKey(),
+ 'count' => CRM_Core_DAO::singleValueQuery($sql, $params)
+ );
+ }
+
}
* @return CRM_Core_DAO|NULL a query-handle (like the result of CRM_Core_DAO::executeQuery)
*/
public function findReferences($targetDao);
+
+ /**
+ * Create a query to find references to a particular record
+ *
+ * @param CRM_Core_DAO $targetDao the instance for which we want references
+ * @return array a record describing the reference; must include the keys:
+ * - 'type': string (not necessarily unique)
+ * - 'count': int
+ */
+ public function getReferenceCount($targetDao);
}
}
}
+ public function getReferenceCount($targetDao) {
+ if (! ($targetDao instanceof CRM_Core_DAO_OptionValue)) {
+ throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao));
+ }
+ if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) {
+ return parent::getReferenceCount($targetDao);
+ } else {
+ return NULL;
+ }
+ }
+
public function getTargetOptionGroupId() {
if ($this->targetOptionGroupId === NULL) {
$this->targetOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->targetOptionGroupName, 'id', 'name');
$this->assertEquals($contact->id, $refDao->contact_id);
}
+ function testGetReferenceCounts() {
+ $result = $this->callAPISuccess('Contact', 'create', array(
+ 'first_name' => 'Testily',
+ 'last_name' => 'McHaste',
+ 'contact_type' => 'Individual',
+ 'api.Email.replace' => array(
+ 'values' => array(
+ array(
+ 'email' => 'spam@dev.null',
+ 'is_primary' => 0,
+ 'location_type_id' => 1,
+ )
+ ),
+ ),
+ 'api.Phone.replace' => array(
+ 'values' => array(
+ array(
+ 'phone' => '234-567-0001',
+ 'is_primary' => 1,
+ 'location_type_id' => 1,
+ ),
+ array(
+ 'phone' => '234-567-0002',
+ 'is_primary' => 0,
+ 'location_type_id' => 1,
+ ),
+ ),
+ ),
+ ));
+
+ $dao = new CRM_Contact_BAO_Contact();
+ $dao->id = $result['id'];
+ $this->assertTrue((bool) $dao->find(TRUE));
+
+ $refCounts = $dao->getReferenceCounts();
+ $this->assertTrue(is_array($refCounts));
+ $refCountsIdx = CRM_Utils_Array::index(array('name'), $refCounts);
+ $this->assertEquals(1, $refCountsIdx['sql:civicrm_email:contact_id']['count']);
+ $this->assertEquals('civicrm_email', $refCountsIdx['sql:civicrm_email:contact_id']['table']);
+ $this->assertEquals(2, $refCountsIdx['sql:civicrm_phone:contact_id']['count']);
+ $this->assertEquals('civicrm_phone', $refCountsIdx['sql:civicrm_phone:contact_id']['table']);
+ $this->assertTrue(!isset($refCountsIdx['sql:civicrm_address:contact_id']));
+ }
+
function composeQueryExamples() {
$cases = array();
// $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');