Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-825

Error on adding contact object to group through API

    Details

    • Type: Bug
    • Status: Done/Fixed
    • Priority: Major
    • Resolution: Fixed/Completed
    • Affects Version/s: 1.3
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      If we do this:
      $contacts = crm_get_group_contacts($group, NULL);

      We get an object (among others) that looks like this:
      [5] => CRM_Contact_DAO_Contact Object
      (
      [id] => 10305
      [domain_id] => 1
      [contact_type] => Individual
      [legal_identifier] =>
      [external_identifier] =>
      [sort_name] => Blah
      [display_name] => Mr. Blah
      [nick_name] =>
      [snip[
      [contact_id] => 167
      [first_name] => Blah
      [snip]
      )

      So far so good. However when we try and insert these contacts into a new group by doing something like this:
      $result = crm_add_group_contacts($addedgroup, $contacts);

      We get the following error:
      Array
      (
      [callback] => Array
      (
      [0] => CRM_Core_Error
      [1] => handle
      )

      [code] => -3
      [message] => DB Error: constraint violation
      [mode] => 16
      [debug_info] => INSERT INTO civicrm_subscription_history (contact_id , group_id , date , method , status , tracking ) VALUES ( 10305 , 23 , 20060321202634 , 'Admin' , 'Added' , NULL ) [nativecode=1216 ** Cannot add or update a child row: a foreign key constraint fails]
      [type] => DB_Error
      [user_info] => INSERT INTO civicrm_subscription_history (contact_id , group_id , date , method , status , tracking ) VALUES ( 10305 , 23 , 20060321202634 , 'Admin' , 'Added' , NULL ) [nativecode=1216 ** Cannot add or update a child row: a foreign key constraint fails]
      [to_string] => [db_error: message="DB Error: constraint violation" code=-3 mode=callback callback=CRM_Core_Error::handle prefix="" info="INSERT INTO civicrm_subscription_history (contact_id , group_id , date , method , status , tracking ) VALUES ( 10305 , 23 , 20060321202634 , 'Admin' , 'Added' , NULL ) [nativecode=1216 ** Cannot add or update a child row: a foreign key constraint fails]"]
      )

      As you can see - the subscription_history code is trying to use the civicrm_group_contact id as the contact_id. This of course fails (and in cases where it doesn't fail is broken anyway, as history for the wrong contact will be entered).

      I am not sure where the problem is - it could be that crm_get_group_contacts is returning invalid CRM_Contact_DAO_Contact objects, or that crm_add_group_contacts is passing the wrong id downstream.

      If it is the latter then it should be as simple as (Group.php, line 209)
      foreach($contacts as $contact){
      if ( ! isset( $contact->id ))

      { return _crm_error( 'Invalid contact object passed in' ); }
      $contactID[] = $contact->id;
      }
      foreach($contacts as $contact){
      if ( ! isset( $contact->contact_id )) { return _crm_error( 'Invalid contact object passed in' ); }

      $contactID[] = $contact->contact_id;
      }

      However, this would of course break if crm_add_group_contacts should actually be expecting a contact object with id referencing contact_id as implied by http://wiki.civicrm.org/confluence/display/CRM/Data+Model#DataModel-ContactRef.

      Hope this helps!

        Attachments

          Activity

            People

            • Assignee:
              lobo Donald A. Lobo
              Reporter:
              grugnog Owen Barton
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: