Pseudocode
1. Identify new phone numbers
2. Create New Entry Objects for each number
3. Create array of entry objects that includes
3.1 existent numbers being edited
3.2 new numbers being added
4. Update all numbers with new/edited values
5. Commit Numbers Entries to the Phone Number Section in DB
6. Update the Member entry to reflect the ID of the new phone number
7. Commit the Member Entry to the Members Section in the DB
I have this bit working which saves new phone numbers to the Phone Number Section (PseudoCode #s 1-5; I'm not worrying about editing existing numbers just yet):
foreach($_POST['fields']['phone-numbers-new'] as $hash => $field)
{
$data = array();
$data[$ids['phoneNumbers']['fields']['type']]['handle'] = General::createHandle($field['type']);
$data[$ids['phoneNumbers']['fields']['type']]['value'] = $field['type'];
$data[$ids['phoneNumbers']['fields']['number']]['handle'] = General::createHandle($field['number']);
$data[$ids['phoneNumbers']['fields']['number']]['value'] = $field['number'];
$data[$ids['phoneNumbers']['fields']['added_by']]['author_id'] = "1";
$data[$ids['phoneNumbers']['fields']['active']]['value'] = "yes";
$newPNs = EntryManager::create();
$newPNs->set('section_id', "{$ids['phoneNumbers']['sectionID']}");
$newPNs->setDataFromPost($data);
$newPNs->setData($ids['phoneNumbers']['fields']['type'],$data[$ids['phoneNumbers']['fields']['type']]);
$newPNs->setData($ids['phoneNumbers']['fields']['number'],$data[$ids['phoneNumbers']['fields']['number']]);
$newPNs->setData($ids['phoneNumbers']['fields']['added_by'],$data[$ids['phoneNumbers']['fields']['added_by']]);
$newPNs->setData($ids['phoneNumbers']['fields']['active'],$data[$ids['phoneNumbers']['fields']['active']]);
$ids['newPNs'][] = $newPNs->get('id');
$newPNs->commit(); //adds the new phone number to the Phone Number Section
}
I then update the Member Entry with the new phone numbers:
$entries = EntryManager::fetch($ids['entry']);
$member = $entries[0];
$data = array(); // reset data array
$data[$ids['phoneNumbers']['fieldID']] = $member->getData($ids['phoneNumbers']['fieldID']);
for($i = 0; $i < count($ids['newPNs']); $i++)
{
$index = count($data[$ids['phoneNumbers']['fieldID']]['relation_id']) + $i;
$data[$ids['phoneNumbers']['fieldID']]['relation_id'][$index] = "{$ids['newPNs'][$i]}";
}
$member->setData($ids['phoneNumbers']['fieldID'],$data[$ids['phoneNumbers']['fieldID']]);
$member->commit();
That worked for adding new phone numbers. Edited numbers should be updated in the Section prior to committing the member Entry and the results are also successful.