Is it possible to get distinct using apiv4? For example $result = civicrm_api4('Contact', 'get' <- getdistinct? Thanks
2 Answers
Depending what you're trying to do you might be able to just use group by, like if you want a list of all the distinct last names in your database:
\Civi\Api4\Contact::get()
->addSelect('last_name')
->addGroupBy('last_name')
->execute();
If you want to get fancy you can create a searchkit, where you set it up with distinct, and then run it via code to get the results. See https://github.com/civicrm/civicrm-core/blob/91ac1dc25c9e24422c6bc9feabca9e5c16b4856d/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php for some fancy examples, but at its simplest you could create the searchkit via the UI, then just run it with something like below. See api4 explorer for more params.
$result = SearchDisplay::Run()
->setSavedSearch('the_saved_search_name')
->setDisplay('the_search_display_name')
->execute();
APIv4 supports distinct when used with aggregation, but doesn't add the keyword DISTINCT to select queries.