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) »
  • Get Smart Group count from DB
Pages: [1]

Author Topic: Get Smart Group count from DB  (Read 879 times)

elcongrio

  • I’m new here
  • *
  • Posts: 26
  • Karma: 0
  • CiviCRM version: 3.6.1
  • CMS version: Wordpress
  • MySQL version: 5.3
  • PHP version: 5.3.3
Get Smart Group count from DB
December 23, 2014, 11:55:57 am
Hi everybody:

I need to develop a dashboard that shows the contact's count from some smart groups.
Through API, if I ask the rowCount for the group, the result is always 1.
I can query the complete list of contacts, and count the array rows in PHP; but this method is quite slow.
There's a way to run the smart group query direct to the DB. Surely this must be much faster.
Thanks in advance.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Get Smart Group count from DB
December 24, 2014, 12:27:28 am
You are calling civicrm_api3("contact","getcount", array("group_id"=>42) ?



-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Get Smart Group count from DB
December 27, 2014, 11:45:10 am
There's a table called "civicrm_group_contact_cache" which stores the list of contacts for smart groups. The table is cleared+rebuilt periodically (although I don't recall the exact rules off the top of my head). I believe you can call  CRM_Contact_BAO_GroupContactCache::check() to ensure that it's up-to-date. For more details on how that cache works, I'd recommend reading more code in CRM_Contact_BAO_GroupContactCache.

The GroupContactCache a relatively low-level service. In practice, I don't think it's changed much in the past few years, but in theory it could change in future releases. Regardless, whether one examines GroupContactCache directly or writes a bug-fix so that the API deals with it better, I think GroupContactCache is an important piece of the puzzle.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Get Smart Group count from DB
December 28, 2014, 11:28:17 am
you have a cronjob that triggers a cache rebuild if you want to (disabled by default if I recall)

Try to run it before using the api explorer to do a contact.getcount, see what it returns
X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

elcongrio

  • I’m new here
  • *
  • Posts: 26
  • Karma: 0
  • CiviCRM version: 3.6.1
  • CMS version: Wordpress
  • MySQL version: 5.3
  • PHP version: 5.3.3
Re: Get Smart Group count from DB
December 30, 2014, 05:26:40 am
I will give a try to both methods

elcongrio

  • I’m new here
  • *
  • Posts: 26
  • Karma: 0
  • CiviCRM version: 3.6.1
  • CMS version: Wordpress
  • MySQL version: 5.3
  • PHP version: 5.3.3
Re: Get Smart Group count from DB
December 30, 2014, 05:28:56 am
Xavier, that was my exact call. Always return 1.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Get Smart Group count from DB

This forum was archived on 2017-11-26.