Trying to get all contacts cases using bean factory

Hi am trying to amend some old code that uses beanFactory. We have an API endpoint that gets a contact and there account as well as some other information. I need to amed this so it get all the contacts cases.

I added in a relationship 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
if ($contactBean->load_relationship('cases')) {
$caseBeans = $contactBean->cases->getBeans();
$parentBean = false;
if (!empty($caseBeans)) {
reset($caseBeans);
foreach($caseBeans as $case) {
$result['cases'][] = $helper->formatForApi($case, array(
'id',
'name',
));
}
}
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

this returns only the first case and not an array of them all

Fullscreen
1
2
3
4
5
6
7
8
9
"cases": [
{
"id": "23a65880-4628-11ed-8f5d-023af1fa0476",
"name": "CASE-81964",
"_acl": {
"fields": {}
}
}
],
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Im not that familiar with bean-factory and am wondering if this would be better written as a custom SQL query, In addition can you get the total number of cases for a contact using bean-factory like a count()