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
- New entity defined -
- DAO for the table
- 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