Saving Contact $bean deletes email addresses

In the after_save logic hook for Accounts I wrote some code that would change the primary address of all Contacts linked to that account to the billing address of that account.

The code works, but we eventually noticed, the the mystery of vanishing email-addresses was directly related to that code I wrote:

$bean->load_relationship('contacts');
foreach ($bean->contacts->getBeans() as $contact){
   ...
  $contact->primary_address_street = $bean->billing_address_street;
  ...
  $contact->save();
}

The intended changes appear, but all linked contacts lose their email addresses.


Any ideas on how to convince $contact->save() to indeed save everything?
Parents
  • Instead of using the getBeans method use get()

    $bean->load_relationship('contacts');
    foreach ($bean->contacts->get() as $contact_id){       $contact = BeanFactory::getBean('Contacts', $contact_id);    if (! empty($contact->id)) {
        $contact->primary_address_street = $bean->billing_address_street;
        ...
        $contact->save();    }
    }

    That should resolve the issue. 
  • Also, I inspected the $contact->emailAddress object before and after $contact->save(). Apart from the Emails being gone, I noticed the lastsql value in $contact->emailAddress->db->lastsql

    Before $contact->save():
    SELECT ea.email_address, ea.email_address_caps, ea.invalid_email, ea.opt_out, ea.date_created, ea.date_modified, ear.id, ear.email_address_id, ear.bean_id, ear.bean_module, ear.primary_address, ear.reply_to_address, ear.deleted
    FROM email_addresses ea LEFT JOIN email_addr_bean_rel ear ON ea.id = ear.email_address_id
    WHERE ear.bean_module = 'Contacts'
    AND ear.bean_id = '40d72dd3-251d-6c84-9ec6-508fd561cccb'
    AND ear.deleted = 0
    ORDER BY ear.reply_to_address, ear.primary_address DESC

    After $contact->save():
    update email_addr_bean_rel set deleted=1 where id in ('47208175-a510-31c5-e28a-508fd5530b13' ,'d0a75bcd-3ba3-db6f-faaa-51c7eabcc2a5') 


    So, it looks like $contact->save() actively deletes email address relationships down the line for some reason ...
Reply
  • Also, I inspected the $contact->emailAddress object before and after $contact->save(). Apart from the Emails being gone, I noticed the lastsql value in $contact->emailAddress->db->lastsql

    Before $contact->save():
    SELECT ea.email_address, ea.email_address_caps, ea.invalid_email, ea.opt_out, ea.date_created, ea.date_modified, ear.id, ear.email_address_id, ear.bean_id, ear.bean_module, ear.primary_address, ear.reply_to_address, ear.deleted
    FROM email_addresses ea LEFT JOIN email_addr_bean_rel ear ON ea.id = ear.email_address_id
    WHERE ear.bean_module = 'Contacts'
    AND ear.bean_id = '40d72dd3-251d-6c84-9ec6-508fd561cccb'
    AND ear.deleted = 0
    ORDER BY ear.reply_to_address, ear.primary_address DESC

    After $contact->save():
    update email_addr_bean_rel set deleted=1 where id in ('47208175-a510-31c5-e28a-508fd5530b13' ,'d0a75bcd-3ba3-db6f-faaa-51c7eabcc2a5') 


    So, it looks like $contact->save() actively deletes email address relationships down the line for some reason ...
Children
No Data