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

Calling crm_create_contact without specifying location_type_id causes unrecoverable error.

    Details

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

      Description

      [I'm not sure what to call this bug, since I'm not sure what correct behavior is here. But Don Lobo thinks this is probably a bug, so see this as a placeholder]

      Attempting to create a contact without specifying a location_type_id parameter causes the DataObject "unrecoverable error" screen to come up.

      The offending code (from petition.module – in techsoldaten's sandbox on drupal.org) looks like this:

      <pre>
      if(strlen($edit['first_name']) && strlen($edit['last_name'])){
      $edit['location_type'] = 1;
      $search = CRM_Core_BAO_UFGroup::findContact( $edit, null, false);
      if(!is_numeric($search))

      { $new = crm_create_contact($edit, 'Individual'); $dat['id'] = $new->id; }

      else

      { // If the contact already exists, this is how it should look $dat['id'] = $search; }

      </pre>

      Note $edit['location_type'] = 1; this was apparently the old name for the property. If crm_create_contact is called this way, you get the following unrecoverable error:

      <pre>
      Array
      (
      [callback] => Array
      (
      [0] => CRM_Core_Error
      [1] => handle
      )

      [code] => -3
      [message] => DB Error: constraint violation
      [mode] => 16
      [debug_info] => INSERT INTO civicrm_location (entity_table , entity_id , is_primary ) VALUES ('civicrm_contact' , 2 , 1 ) [nativecode=1216 ** Cannot add or update a child row: a foreign key constraint fails]
      [type] => DB_Error
      [user_info] => INSERT INTO civicrm_location (entity_table , entity_id , is_primary ) VALUES ('civicrm_contact' , 2 , 1 ) [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_location (entity_table , entity_id , is_primary ) VALUES ('civicrm_contact' , 2 , 1 ) [nativecode=1216 ** Cannot add or update a child row: a foreign key constraint fails]"]
      )
      </pre>

      Changing $edit['location_type'] = 1; to $edit['location_type_id'] = 1; appears to be a work-around, although likely better behavior would for the API to supply a reasonable default.

      In any case: the error needs to get caught higher up, since the unrecoverable error message doesn't really help the developer figure out the problem (without spending hours in the debugger, of course

      I enclose the current version of the module, since a part of the issue is likely how to document the right way to create a contact.

        Attachments

          Activity

            People

            • Assignee:
              lobo Donald A. Lobo
              Reporter:
              torenware Rob Thorne
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: