1

Currently I am using API for fetch the data.
I am creating an extension. I want to fetch the particular column's data from the table. How will I get the data?

//getting id and name
public static function getName(){
 $data = ($id,$name);
  $result = civicrm_api3('Emp', 'get', [
      'sequential' => 1,
      'return' => ["id", "name"],
  ]);

  foreach ($result['values'] as $value) {
      $data[$id] += $value['id'];
      $data[$name] += $value['name'];

  }
  return $data;
} 

Select option using BuildQuickForm

$this->add('select', 
      'name',         //field type 
      'name', //field label
      $this->getName() //list (id, name) 
      );

enter image description here

3

For every table CiviCRM has api, you can use following method to retrieve data by specifying return in api params.

1 - Get - Returns one or more results and with N number of columns

$result = civicrm_api3('Contact', 'get', [
  'return' => ["id", "display_name"],
  'contact_sub_type' => "Parent",
]);

2 - GetValue - Returns only single result and with single column (make sure to use id in filer or limit =1 else the api will throw error if the api returns more than one results)

$result = civicrm_api3('Contact', 'getvalue', [
  'return' => "display_name",
  'id' => 1,
]);

3 - GetSingle - Returns only single result and with N number of columns (make sure to use id in filer or limit =1 else the api will throw error if the api returns more than one results)

$result = civicrm_api3('Contact', 'getsingle', [
  'return' => ["display_name", "email"],
  'id' => 1,
]);

Incase if you want to retrieve from custom table created using extension then you will need to make sure you have

  1. New entity defined -
  2. DAO for the table
  3. api with create, delete and get action

You can use Civix to create new entity which will create above 3 points automatically for you.

Then you can use below snippet to build your select field

    $this->add('select', 
      'name',         //field type 
      ts('Name'), //field label
      CRM_Utils_Array::collect('name', civicrm_api3('Emp', 'get', [
        'return' => ["id", "name"],
      ])['values']) //list (id, name) 
    );

HTH

Pradeep

  • I have an id and user_name columns in table. I want to fetch and put as option value i.e. <option value='id'>user_name </option> – deepak Jan 22 at 10:32
  • Which table you talking about? Is it a civi table or your own table? – Pradeep Nayak Jan 22 at 10:49
  • My custom table. Check the above image. – deepak Jan 22 at 10:53
  • 1
    You can get idea how this works at github.com/civicrm/civicrm-core/blob/master/CRM/Utils/… . So can you please mark this as answered my answer solves your problem? – Pradeep Nayak Jan 22 at 13:30
  • 1
    I think you voted up rather marking it as answered – Pradeep Nayak Jan 22 at 15:33

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.