Using SugarBeanApiHelper with relationships

Can someone offer advice our point me to some documentation

Im trying to retrieve a contact and there related account details, I dont want to retreive everything as I only need selected fields and also want to reduce overheads. I can get this doing an sql request but wanted to understand more how to do things with beanFactory

I have the following

        $contactBean = BeanFactory::retrieveBean('Contacts', $args['ID']);

        if ($contactBean) {
            if ($contactBean->load_relationship('accounts')) {
               $accountBean = $contactBean->accounts->getBeans();
            }

            $helper = new SugarBeanApiHelper($api);
            $result['success'] = true;
            $result['records'] = $helper->formatForApi($contactBean, array('id', 'name', 'portaluser_c'));
            return $result;
        }

        $result['errors'] = "Contact details not found.";
        return $result;

The relationship is working but Im stuck on how i then include the accounts details i want with $helper->formatForApi

Do i need to crate a new bean for the account? or am i going about this the wrong way?

  • changed my code to the following

            $contactBean = BeanFactory::retrieveBean('Contacts', $args['ID']);

            if ($contactBean) {
                if ($contactBean->load_relationship('accounts')) {
                   $accountBeans = $contactBean->accounts->getBeans();
                   $parentBean = false;
                   if (!empty($accountBeans)) {
                       reset($accountBeans);
                       $parentBean = current($accountBeans);
                   }
                }

                $helper = new SugarBeanApiHelper($api);
                $result['success'] = true;
                $result['contact'] = $helper->formatForApi($contactBean, array('id', 'name', 'portaluser_c'));
                $result['account'] = $helper->formatForApi($parentBean, array('id', 'name'));
                return $result;
            }

            $result['errors'] = "Contact details not found.";
            return $result;

    this is getting me the fields i wanted but was wandering if there was a way to only use $helper->formatForApi once and fetch a combined array or is this the only way to do it?