Skip to content

Instantly share code, notes, and snippets.

@chadhutchins
Created September 25, 2013 15:56
Show Gist options
  • Save chadhutchins/6701768 to your computer and use it in GitHub Desktop.
Save chadhutchins/6701768 to your computer and use it in GitHub Desktop.
Example of issues grabbing the account_id of a contact when using BeanFactory in version 6.5.15.
<?php
// Having issues grabbing the account_id of a contact when using BeanFactory
// in version 6.5.15 to create/load the contact
// To test this out, put this in custom/modules/Contacts/test.php and go
// to http://instance/index.php?module=Contacts&action=test to see the results
// Only "CASE 4" below returns the expected results.
$account_id = "11c5a47e-49a8-c077-4c6a-524304ac91ca";
// create contact
$contact = BeanFactory::getBean('Contacts');
$contact->first_name = "test";
$contact->last_name = "test";
$contact->save();
// create a copy of the id to use later
$new_contact_id = $contact->id;
echo "new contact id: {$new_contact_id}<br>";
// relate the contact to the account
$account = BeanFactory::getBean('Accounts',$account_id);
$account->load_relationship('contacts');
$account->contacts->add($contact->id);
$account->save();
// CASE 1: try to get the account_id for a contact
echo "Case #1: {$contact->account_id}<br>";
// CASE 2: try loading relationship then get the account_id
$contact->load_relationship('accounts');
echo "Case #2: {$contact->account_id}<br>";
// CASE 3: get a new copy of the contact using BeanFactory
$contact = BeanFactory::getBean('Contacts',$new_contact_id);
$contact->load_relationship('accounts');
echo "Case #3: {$contact->account_id}<br>";
// CASE 4: get a new contact the old way not using BeanFactory
$contact = new Contact();
$contact->retrieve($new_contact_id);
$contact->load_relationship('accounts');
echo "Case #4: {$contact->account_id}<br>";
// CASE 5: try again using a new copy of the contact using BeanFactory
$contact = BeanFactory::getBean('Contacts',$new_contact_id);
$contact->load_relationship('accounts');
echo "Case #5: {$contact->account_id}<br>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment