Skip to content

Commit

Permalink
APIv4 - Use non-deprecated join syntax internally
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jun 28, 2021
1 parent c0f9cc6 commit 26a7b54
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
12 changes: 6 additions & 6 deletions CRM/Event/Page/UserDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function listParticipations() {
$event_rows = [];

$participants = \Civi\Api4\Participant::get(FALSE)
->addSelect('id', 'contact_id', 'status_id:name', 'status_id:label', 'event.id', 'event.title', 'event.start_date', 'event.end_date')
->addSelect('id', 'contact_id', 'status_id:name', 'status_id:label', 'event_id', 'event_id.title', 'event_id.start_date', 'event_id.end_date')
->addWhere('contact_id', '=', $this->_contactId)
->addOrderBy('event.start_date', 'DESC')
->addOrderBy('event_id.start_date', 'DESC')
->execute()
->indexBy('id');

Expand All @@ -39,10 +39,10 @@ public function listParticipations() {
$p['participant_id'] = $p['id'];
$p['status'] = $p['status_id:name'];
$p['participant_status'] = $p['status_id:label'];
$p['event_id'] = $p['event.id'];
$p['event_title'] = $p['event.title'];
$p['event_start_date'] = $p['event.start_date'];
$p['event_end_date'] = $p['event.end_date'];
$p['event_id'] = $p['event_id'];
$p['event_title'] = $p['event_id.title'];
$p['event_start_date'] = $p['event_id.start_date'];
$p['event_end_date'] = $p['event_id.end_date'];

$event_rows[] = $p;
}
Expand Down
6 changes: 3 additions & 3 deletions Civi/Api4/Service/Schema/Joinable/CustomGroupJoinable.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function getEntityFields() {
$entityFields = (array) \Civi::cache('metadata')->get($cacheKey);
if (!$entityFields) {
$fields = CustomField::get(FALSE)
->setSelect(['custom_group.name', 'custom_group.extends', 'custom_group.table_name', 'custom_group.title', '*'])
->addWhere('custom_group.table_name', '=', $this->getTargetTable())
->setSelect(['custom_group_id.name', 'custom_group_id.extends', 'custom_group_id.table_name', 'custom_group_id.title', '*'])
->addWhere('custom_group_id.table_name', '=', $this->getTargetTable())
->execute();
foreach ($fields as $field) {
$entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, self::getEntityFromExtends($field['custom_group.extends']));
$entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, self::getEntityFromExtends($field['custom_group_id.extends']));
}
\Civi::cache('metadata')->set($cacheKey, $entityFields);
}
Expand Down
8 changes: 4 additions & 4 deletions Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public static function arrayToField(array $data, $entity) {
if (!empty($data['custom_group_id'])) {
$field = new CustomFieldSpec($data['name'], $entity, $dataTypeName);
if (strpos($entity, 'Custom_') !== 0) {
$field->setName($data['custom_group.name'] . '.' . $data['name']);
$field->setName($data['custom_group_id.name'] . '.' . $data['name']);
}
else {
$field->setTableName($data['custom_group.table_name']);
$field->setTableName($data['custom_group_id.table_name']);
}
$field->setColumnName($data['column_name']);
$field->setCustomFieldId($data['id'] ?? NULL);
$field->setCustomGroupName($data['custom_group.name']);
$field->setCustomGroupName($data['custom_group_id.name']);
$field->setTitle($data['label']);
$field->setLabel($data['custom_group.title'] . ': ' . $data['label']);
$field->setLabel($data['custom_group_id.title'] . ': ' . $data['label']);
$field->setHelpPre($data['help_pre'] ?? NULL);
$field->setHelpPost($data['help_post'] ?? NULL);
if (self::customFieldHasOptions($data)) {
Expand Down
10 changes: 5 additions & 5 deletions Civi/Api4/Service/Spec/SpecGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ private function addCustomFields($entity, RequestSpec $specification, $values =
$extends = $customInfo['extends'];
}
$customFields = CustomField::get(FALSE)
->addWhere('custom_group.extends', 'IN', $extends)
->addWhere('custom_group.is_multiple', '=', '0')
->setSelect(['custom_group.name', 'custom_group.title', '*'])
->addWhere('custom_group_id.extends', 'IN', $extends)
->addWhere('custom_group_id.is_multiple', '=', '0')
->setSelect(['custom_group_id.name', 'custom_group_id.title', '*'])
->execute();

foreach ($customFields as $fieldArray) {
Expand All @@ -145,8 +145,8 @@ private function addCustomFields($entity, RequestSpec $specification, $values =
*/
private function getCustomGroupFields($customGroup, RequestSpec $specification) {
$customFields = CustomField::get(FALSE)
->addWhere('custom_group.name', '=', $customGroup)
->setSelect(['custom_group.name', 'custom_group.table_name', 'custom_group.title', '*'])
->addWhere('custom_group_id.name', '=', $customGroup)
->setSelect(['custom_group_id.name', 'custom_group_id.table_name', 'custom_group_id.title', '*'])
->execute();

foreach ($customFields as $fieldArray) {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/ACLPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ public function testContactGetViaJoin($version) {
$tag2 = $this->tagCreate(['name' => uniqid('other'), 'created_id' => $other])['id'];
$this->setPermissions(['access CiviCRM']);
$this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereHookAllResults']);
$createdFirstName = $version === 4 ? 'created.first_name' : 'created_id.first_name';
$createdFirstName = 'created_id.first_name';
$result = $this->callAPISuccess('Tag', 'get', [
'check_permissions' => 1,
'return' => ['id', $createdFirstName],
Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/api/v4/Query/PermissionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ public function testImplicitJoinPermissions() {
0)
->execute()->first();
$participant = Participant::get()
->addSelect('contact.first_name', 'event.title')
->addWhere('event.id', '=', $event['id'])
->addSelect('contact_id.first_name', 'event_id.title')
->addWhere('event_id.id', '=', $event['id'])
->execute()
->first();

$this->assertEquals('ABC123 Event', $participant['event.title']);
$this->assertEquals($name, $participant['contact.first_name']);
$this->assertEquals('ABC123 Event', $participant['event_id.title']);
$this->assertEquals($name, $participant['contact_id.first_name']);

// Remove access to view events
$config->userPermissionClass->permissions = [
Expand All @@ -107,14 +107,14 @@ public function testImplicitJoinPermissions() {
'view event participants',
];
$participant = Participant::get()
->addSelect('contact.first_name')
->addSelect('event.title')
->addSelect('contact_id.first_name')
->addSelect('event_id.title')
->addWhere('id', '=', $contact['participant']['id'])
->execute()
->first();

$this->assertTrue(empty($participant['event.title']));
$this->assertEquals($name, $participant['contact.first_name']);
$this->assertTrue(empty($participant['event_id.title']));
$this->assertEquals($name, $participant['contact_id.first_name']);

}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/api/v4/Spec/SpecFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function testCustomFieldWillBeReturned() {

$data = [
'custom_group_id' => $customGroupId,
'custom_group.name' => 'my_group',
'custom_group.title' => 'My Group',
'custom_group_id.name' => 'my_group',
'custom_group_id.title' => 'My Group',
'id' => $customFieldId,
'name' => $name,
'label' => $name,
Expand Down

0 comments on commit 26a7b54

Please sign in to comment.