Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow API4 match to match an empty value #22882

Merged
merged 2 commits into from
Mar 9, 2022

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Mar 3, 2022

Overview

The new matching functionality on X::save is nice but I found an issue that I find counterintuitive / confusing.

For example if you have some data to import and some has an external_identifier then you might match on first_name,last_name,external_identifier but if external_identifier is empty it won't match because it does external_identifier = '' and that's not the same as external_identifier IS NULL

ie. https://github.com/civicrm/civicrm-core/blob/master/Civi/Api4/Generic/AbstractSaveAction.php#L145

@colemanw said: Maybe patch that line to use the IS EMPTY operator if $val === ''?

Pending test to be added here:

->setMatch(['first_name', 'external_identifier'])

Before

Matching on an empty string doesn't match.

Eg. match on: first_name='bob'; external_identifier=''; creates new record even if that exists.

After

Matching on an empty string matches.

Eg. match on: first_name='bob'; external_identifier=''; updates existing record.

Technical Details

This is particularly noticeable when doing imports.

Comments

@colemanw I've added a test now.

@civibot
Copy link

civibot bot commented Mar 3, 2022

(Standard links)

@civibot civibot bot added the master label Mar 3, 2022
@mattwire mattwire marked this pull request as ready for review March 8, 2022 19:18
@colemanw
Copy link
Member

colemanw commented Mar 8, 2022

Thanks @mattwire - Jenkins says you have to add spaces around the "<" on line 56 of the test.

@mattwire mattwire changed the title WIP Allow API4 match to match an empty value Allow API4 match to match an empty value Mar 8, 2022
@colemanw
Copy link
Member

colemanw commented Mar 9, 2022

@mattwire your new test didn't pass

@mattwire
Copy link
Contributor Author

mattwire commented Mar 9, 2022

@colemanw This seems to be a setup/teardown issue. When the test is run by itself it passes every time. But when run as a suite (eg. api/v4/Action) it fails suggesting that something external is interfering with the test?

@colemanw
Copy link
Member

colemanw commented Mar 9, 2022

@mattwire I think the match criteria isn't unique enough, and running the test when there's other data in the database, you get more than one match for a contact with first name "Abc" and a blank external identifier.

@colemanw
Copy link
Member

colemanw commented Mar 9, 2022

That works. Typically in tests requiring unique names I use something like $lastName = uniqid(__FUNCTION__);. That's pretty bulletproof even when running on a database with lots of existing data.

@colemanw colemanw merged commit ec4cc2e into civicrm:master Mar 9, 2022
@highfalutin
Copy link
Contributor

Thank you for this work, @mattwire and @colemanw! I found myself running into this exact issue just a couple days ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants