To fetch a list of records, use the Model::getList() method. It returns an instance of ListResult class.
$model = $client->model('Contact');
$result = $model->getList([], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "name": "Kristina Abramowitz",
5 "first_name": "Kristina",
6 "id": "7eba2a01-6cb6-f38c-d4c2-5a8fc2def9d2",
7 "last_name": "Abramowitz",
9 "_display": "Kristina Abramowitz"
12 "name": "Garret Alejo",
13 "first_name": "Garret",
14 "id": "346ba8d2-2851-166b-13e6-5a8fc2c18299",
17 "_display": "Garret Alejo"
Here we specify that we want to start from first record (0 is passed in $offset
argument), and we want no more than 2 rescords returned (2 is passed in $limit
argument). totalResults() returned 200, telling us that there is more data available. If we want to retrieve next 2 records, we have to make another request, passing 2 in $offset
:
$result = $model->getList([], 2, 2);
Model::getist() accepts an array of options that can be used to refine the request.
filter_text option
Use the filter_text
option for generic text search:
$model = $client->model('Contact');
$result = $model->getList(['filter_text' => 'Michael'], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "name": "Michael Whitehead",
5 "first_name": "Michael",
6 "id": "4ec8fd9d-d035-839e-5356-5a8fc2480082",
7 "last_name": "Whitehead",
9 "_display": "Michael Whitehead"
filters option
Use the filters
option to filter data:
$model = $client->model('Contact');
$result = $model->getList(['filters' => ['address_city' => 'San Jose']], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "name": "Kristina Abramowitz",
5 "first_name": "Kristina",
6 "id": "7eba2a01-6cb6-f38c-d4c2-5a8fc2def9d2",
7 "last_name": "Abramowitz",
9 "_display": "Kristina Abramowitz"
14 "id": "c1edefc7-5595-e61e-0fd9-5a8fc2333dcc",
17 "_display": "Emma Alley"
Another example:
$model = $client->model('Contact');
$result = $model->getList(['filters' => ['filter_favorites' => true]], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "name": "Kristina Abramowitz",
5 "first_name": "Kristina",
6 "id": "7eba2a01-6cb6-f38c-d4c2-5a8fc2def9d2",
7 "last_name": "Abramowitz",
9 "_display": "Kristina Abramowitz",
order option
Use the order
option to return the data in specific order:
$model = $client->model('Contact');
$result = $model->getList(['order' => 'last_name DESC'], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "name": "Arron Ybanez",
6 "id": "a8a157b1-a20a-3023-fc7a-5a8fc28d2b4a",
9 "_display": "Arron Ybanez"
13 "first_name": "Dario",
14 "id": "15a7fc37-2711-5b54-af16-5a8fc27fb778",
17 "_display": "Dario Yan"
fields option
Use the fields
option to specify a list of fields you want to fetch:
$model = $client->model('Contact');
$result = $model->getList(['fields' => ['first_name', 'last_name', 'primary_address_city']], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "first_name": "Kristina",
5 "last_name": "Abramowitz",
6 "primary_address_city": "San Jose",
7 "id": "7eba2a01-6cb6-f38c-d4c2-5a8fc2def9d2",
8 "name": "Kristina Abramowitz",
10 "_display": "Kristina Abramowitz"
13 "first_name": "Garret",
15 "primary_address_city": "Cupertino",
16 "id": "346ba8d2-2851-166b-13e6-5a8fc2c18299",
17 "name": "Garret Alejo",
19 "_display": "Garret Alejo"
query_favorite option
Use the query_favorite
option if you want a flag returned for each record telling if the record was added to favorites:
$model = $client->model('Contact');
$result = $model->getList(['query_favorite' => true], 0, 2);
echo $result->totalResults(), "\n";
echo json_encode($result->getRecords(), JSON_PRETTY_PRINT);
4 "name": "Kristina Abramowitz",
5 "first_name": "Kristina",
6 "id": "7eba2a01-6cb6-f38c-d4c2-5a8fc2def9d2",
7 "last_name": "Abramowitz",
9 "_display": "Kristina Abramowitz",
13 "name": "Garret Alejo",
14 "first_name": "Garret",
15 "id": "346ba8d2-2851-166b-13e6-5a8fc2c18299",
18 "_display": "Garret Alejo",
- Attention
- Records added to favorites will have
favorite
field with value of "0"