public function findByAccountId($accountId, $excludeContactId = null, $arrayResult = true, $onlyFetchMainAccounts = true)
{
$qb = $this->createQueryBuilder('c');
// Only fetch main accounts
if ($onlyFetchMainAccounts) {
$qb->join('c.accountContacts', 'accountContacts', 'WITH', 'accountContacts.main = true');
} else {
$qb->join('c.accountContacts', 'accountContacts');
}
$qb->join('accountContacts.account', 'account', 'WITH', 'account.id = :accountId')->setParameter('accountId', $accountId);
if (!is_null($excludeContactId)) {
$qb->where('c.id != :excludeId')->setParameter('excludeId', $excludeContactId);
}
$query = $qb->getQuery();
if ($arrayResult) {
return $query->getArrayResult();
} else {
return $query->getResult();
}
}