civicrm / org.civicrm.api4

CiviCRM api version 4
Other
8 stars 19 forks source link

Can we 'removeWhere' #203

Open eileenmcnaughton opened 4 years ago

eileenmcnaughton commented 4 years ago

I'm just looking at this code

https://github.com/civicrm/civicrm-core/pull/15456/files#diff-eba7056035c65245b5dd9bf4ccac8fe4R437

Which does 2 api calls with the same parameters except one less the second time - a 'removeWhere' would make it shorter....

@colemanw @totten any ideas on nice ways here?

colemanw commented 4 years ago

@eileenmcnaughton I'm not a big fan of that; I'm worried that removeWhere would get confused with addWhere('NOT', ...) which is a thing. Technically you can setWhere to a different value, so maybe the most readable version of this code would be:

    $contributionWhereClause = [
      ['contribution_recur_id', '=', $id],
      ['is_test', '=', $is_test],
    ];
    $contributionApi = \Civi\Api4\Contribution::get()
      ->setWhere($contributionWhereClause)
      ->addWhere('is_template', '=', 1)
      ->addOrderBy('id', 'DESC')
      ->setLimit(1);
    // First look for new-style template contribution with is_template=1
    $templateContributions = $contributionApi->execute();
    if (!$templateContributions->count()) {
      // Fall back to old style template contributions without is_template
      $templateContributions = $contributionApi->setWhere($contributionWhereClause)->execute();
    }