if ($fkClass) {
$tableName = AllCoreTables::getTableForClass($fkClass);
$fkKey = $data['FKKeyColumn'] ?? 'id';
- // Fixme: Clumsy string manipulation to transform e.g. "contact_id" to "contact" - we never should have done this
- $alias = str_replace('_id', '', $field);
- $joinable = new Joinable($tableName, $fkKey, $alias);
+ // Backward-compatibility for older api calls using e.g. "contact" instead of "contact_id"
+ if (strpos($field, '_id')) {
+ $alias = str_replace('_id', '', $field);
+ $joinable = new Joinable($tableName, $fkKey, $alias);
+ $joinable->setJoinType($joinable::JOIN_TYPE_MANY_TO_ONE);
+ $joinable->setDeprecated();
+ $table->addTableLink($field, $joinable);
+ }
+ $joinable = new Joinable($tableName, $fkKey, $field);
$joinable->setJoinType($joinable::JOIN_TYPE_MANY_TO_ONE);
$table->addTableLink($field, $joinable);
}
return parent::setUpHeadless();
}
- public function testContactJoin() {
-
+ public function testContactJoinDeprecated() {
$contact = $this->getReference('test_contact_1');
$entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
foreach ($entitiesToTest as $entity) {
$results = civicrm_api4($entity, 'get', [
'where' => [['contact_id', '=', $contact['id']]],
+ // Deprecated syntax (new syntax is `contact_id.*` not `contact.*`)
'select' => ['contact.*_name', 'contact.id'],
]);
foreach ($results as $result) {
}
}
+ public function testContactJoin() {
+ $contact = $this->getReference('test_contact_1');
+ $entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
+
+ foreach ($entitiesToTest as $entity) {
+ $results = civicrm_api4($entity, 'get', [
+ 'where' => [['contact_id', '=', $contact['id']]],
+ 'select' => ['contact_id.*_name', 'contact_id.id'],
+ ]);
+ foreach ($results as $result) {
+ $this->assertEquals($contact['id'], $result['contact_id.id']);
+ $this->assertEquals($contact['display_name'], $result['contact_id.display_name']);
+ }
+ }
+ }
+
public function testJoinToPCMWillReturnArray() {
$contact = Contact::create()->setValues([
'preferred_communication_method' => [1, 2, 3],