CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Null or exception when no value found for getsingle call?
Pages: [1]

Author Topic: Null or exception when no value found for getsingle call?  (Read 488 times)

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Null or exception when no value found for getsingle call?
May 04, 2015, 02:59:53 pm
$membership = civicrm_api3('Membership', 'getsingle', array(
   'id' => $membership_id
);

I have a report that if no membership is found this code is raising an exception rather than returning null. Shouldn't this return null if none found? Or should we be putting try/catch around each API call to deal with no return values?
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Null or exception when no value found for getsingle call?
May 04, 2015, 09:31:33 pm
If you use 'getsingle' it will throw an Exception if it does not find exactly 1 result. If you use 'get' it will return what it finds. (getvalue is in the same camp as getsingle).

So, it depends on what you are doing which you use. Being able to use getsingle with a try catch can be pretty handy - but if you don't care if none exist then use get instead.

Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: Null or exception when no value found for getsingle call?
May 05, 2015, 11:09:00 am
Thanks. I hadn't realized there was supposed to be variations in error handling.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Null or exception when no value found for getsingle call?

This forum was archived on 2017-11-26.