0

experimenting with refactoring some old code and figured I would experiment with apiv4. the first hurdle was figuring out how to use the ArrayObject that is returned.

in apiv3, for example I would look at the count value in the array. in apiv4 there is no such value.

my api v3 call looks like this

  $result = civicrm_api3('Contact', 'get', array(
  'return' => array("custom_90", "custom_91", "custom_85", "custom_88", "custom_143"),
    'id' => $alum_cid,
    'custom_143' => $sal['id'],
     ));   
  if ($result['count'] != 0) {
    do things
  }                 

my apiv4 call looks like this

$results = \Civi\Api4\Contact::get()
  ->setSelect([
    'Employment_History.Salary_Yearly_', 
    'Employment_History.Currency', 
    'Employment_History.Employer', 
    'Employment_History.End_Date', 
    'Employment_History.relationshipID'
  ])
  ->addWhere('id', '=', $alum_cid)
  ->addWhere('Employment_History.relationshipID', '=', $sal['id'])
  ->setLimit(25)
  ->execute();

how do I get the count of the results?

0

the way the api works now, the thing returned is an ArrayObject which you can call functions on, one of those is count()

so just count the results; and then loop over them as you would if it was a normal array

  if (count($results) != 0) {
    foreach ($results as $result) {
      do things
    } 

  }                 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.