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) »
  • php chain to get contacts with tag name -solved
Pages: [1]

Author Topic: php chain to get contacts with tag name -solved  (Read 2278 times)

bouton

  • Guest
php chain to get contacts with tag name -solved
November 10, 2011, 05:12:45 am
Hi
Trying to figure out the php api chain to get all contacts with a certain tag. I thought it would be easy but it is defeating me. I can get all contacts or I can get the tag - but can't seem to combine them...
Have perused the forums and the book. Quite a few chaining examples on create - but not so many on get - and especially getting Tags.
Thanks. Help/suggestions would be appreciated.

What I have:
<code>
funtion getContacts($tagName) {
civicrm_initialize();
$params = array(
'version' => 3,
'is_tagset' => '0',
'name' => $tagName
// this chaining doesn't work
//,
//   'api.contact.get' => array(
//      'tag_id'=> ????
//   )
);

$results=civicrm_api("Tag","getsingle", $params);
return $results
</code>
« Last Edit: November 11, 2011, 06:48:26 am by bouton »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: php chain to get contacts with tag name
November 10, 2011, 05:50:26 am
The param is tag, not tag_id (that's a mistake IMO)

api.contact.get (tag_id=14) would return the 25 first contacts that are with the tag 14.

not 100% sure about the syntax when chaining, but
tag_id => "$id" (between quote) should work. (or $tag_id?)

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

bouton

  • Guest
Re: php chain to get contacts with tag name
November 10, 2011, 06:27:46 am
This works
I get contactss returned which have tag 10
Code: [Select]
$contactParams = array(
'version' => 3,
'tag'=> '10'
);
$contactResults=civicrm_api("Contact","get", $contactParams);

this does not
Code: [Select]
civicrm_initialize();
$contactParams = array(
       'version' => 3,
'name' => $tag, // from function
'api.contact.get' => array(
'tag'=> '$id' nor '$tag_id'
)
);
error = DB Error: no such field
« Last Edit: November 10, 2011, 06:58:32 am by bouton »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: php chain to get contacts with tag name
November 10, 2011, 09:28:58 am
Let's wait for Eileen in chain that will save us.

In the meantime, you can surrender and simply have two calls one after the other.

X+

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: php chain to get contacts with tag name
November 10, 2011, 12:30:46 pm
If you simply use

civicrm_initialize();
$contactParams = array(
       'version' => 3,
   'name' => $tag, // from function
   'api.contact.get' => 1
   )
   );

$contactResults=civicrm_api("Tag","get", $contactParams);

The the tag id from the outer tag get will be based into the inner contact get as 'tag_id'

For more specific control you can use '$value ' per

http://svn.civicrm.org/civicrm/trunk/api/v3/examples/Contact/APIChainedArrayValuesFromSiblingFunction.php

(there are a few examples in that folder that give variants of it)
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: php chain to get contacts with tag name
November 10, 2011, 12:45:29 pm
so

api.contact_get (array(tag=>'$value.id')) should make it.

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

bouton

  • Guest
Re: php chain to get contacts with tag name -solved
November 11, 2011, 06:48:07 am
Thanks!
I had given up and done two calls  :) but really wanted to try out that new chaining.
So this does work
Code: [Select]
civicrm_initialize();
$params = array(
  'version' => 3,
  'name' => $tagname, // coming in from function
  'api.contact.get' => array(
     'tag'=> '$value.id'
   )
);
$results=civicrm_api("Tag","get", $params);

The contacts then come from
$tag_id = $results['id'];
$contactResults = $results['values'][$tag_id]['api.contact.get']['values'];

FYI doesn't work with $results=civicrm_api("Tag","getsingle", $params);

Thanks
-K
« Last Edit: November 11, 2011, 06:57:03 am by bouton »

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: php chain to get contacts with tag name -solved
November 11, 2011, 11:34:52 am
NB - the reason this was so tricky was because tag get SHOULD support tag_id & it appears it only supports tag. Should be an easy fix
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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • php chain to get contacts with tag name -solved

This forum was archived on 2017-11-26.