CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • debug api Joomla 2.5 PHP
Pages: [1]

Author Topic: debug api Joomla 2.5 PHP  (Read 1315 times)

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
debug api Joomla 2.5 PHP
July 25, 2013, 09:31:25 am
Hi

I have just started using civicrm and need to integrate a custom Joomla 2.5 component into civicrm. I need to automatically add contacts and their contact details, this I can do with the civicrm_api calls.

I can insert a new contact and their details using a nested api call, great! I have problems when trying to update an existing user - I can find them and get their id from their email address.

This works, I then put the return value in $emailid and use this in the api call to identify the id for that person which I then use in the next call to create a contact and pass the id.

This is my call

civicrm_api('Contact', 'Create', array('version' => 3, 'id'=>$emailid, 'contact_type' => $contact_type, 'contact_sub_type'=>$contact_sub_type, 'first_name'=>$firstname, 'last_name'=>$lastname));

I get the following error :

[error_code] => constraint violation
    [sql] => INSERT INTO civicrm_log (entity_table , entity_id , data , modified_id , modified_date ) VALUES ('civicrm_contact' ,  11 , 'civicrm_contact,11' ,  11 ,  20130725160025 )  [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`coolearth`.`civicrm_log`, CONSTRAINT `FK_civicrm_log_modified_id` FOREIGN KEY (`modified_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE)]
    [tip] => add debug=1 to your API call to have more info about the error
    [is_error] => 1
    [error_message] => DB Error: constraint violation


I have tried to add debug=1 to the call but can't find where to add it to give any results and I don't know what is causing the error.

a: Am I going about this task the correct way? If not how should I tackle this?
b: If I am going about it the correct way how can I sort this issue please?


Any help would be gratefully received  :)

I hope you can understand what I am asking without examples.

Thanks

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: debug api Joomla 2.5 PHP
July 25, 2013, 10:04:39 am

can u try logging out and logging in again. seems like there might be some stale data in the session

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
Re: debug api Joomla 2.5 PHP
July 26, 2013, 12:57:47 am
I have logged off and this has not made any difference, sorry. This only happens when I am trying to update the contact details.

I am using a script that is bootstrapped to both Joomla 2.5 and civicrm if that makes any difference.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: debug api Joomla 2.5 PHP
July 26, 2013, 02:01:06 am
Are your MySQL user rights OK?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
Re: debug api Joomla 2.5 PHP
July 26, 2013, 03:18:55 am
Hi

I am working on a internal test environment and the user is root (this will be changed for a live install).

Thanks

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: debug api Joomla 2.5 PHP
July 26, 2013, 03:27:18 am
Hmmm strange, do not know this issue. Your API call looks fine to me. Do you get a DB error when you add an individual directly in the UI of CiviCRM?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
Re: debug api Joomla 2.5 PHP
July 26, 2013, 04:17:22 am
Hi

No, I have just added a contact in the UI of CiviCrm and the entry has gone in fine.

I do have 2 additional contact types of Individual that I have added using the UI, might this cause an issue?

Thanks

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: debug api Joomla 2.5 PHP
July 26, 2013, 04:27:01 am
No, that should not be a problem. Have you tried adding the contact in the UI as the sub type you have used?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
Re: debug api Joomla 2.5 PHP
July 26, 2013, 06:20:18 am
Yes I added it as my first type.

I have been re-organising my code and also now have more data in the email table, I have returned an id value from the api email call using
 
$email_type = civicrm_api('Email','Get',array('email' => $array['email'],  'version' =>3, 'return' => array('email', 'id')));


What I really need to get returned is the contact_id (FK for the contact table) not necessarily the id. I have tried to call it the same way as the id but I don't seem to be able to reference it to pass the value to the update contact call. I am puzzled why I can't reference the contact_id from the email table.

Would this have caused the MySQL error as it was referencing a different id hence person?

Thanks

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: debug api Joomla 2.5 PHP
July 26, 2013, 06:29:06 am
This seems weird:
Code: [Select]
$array['email'],  'version' =>3, 'return' => array('email', 'id')));
If you already know the email_id I would do the following:
Code: [Select]
$apiParams = array('version'=>3, 'id' => $email_id);
$apiEmail = civicrm_api('Email', 'Getsingle', $apiParams);

This will return you only one record, there should be a result element called contact_id?

Correcting your call I would do this:
Code: [Select]
$apiParams = array('version'=>3, 'options' => array('return' => 'contact_id'));
$apiEmail = civicrm_api('Email', 'Get', $apiParams);

That should do the trick (although I have not tested it just now.....)
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
Re: debug api Joomla 2.5 PHP
July 26, 2013, 07:20:35 am
Hi

The first call set you suggested worked fine, the second set unfortunately did not return the contact_id.


Could you confirm or not that email addresses are unique in civicrm please? If they are a single record works perfectly, if not.....  :'(

Thank you

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: debug api Joomla 2.5 PHP
July 26, 2013, 11:53:51 am
An email address is unique by id, not by email address necssarily. So if you have the email id you are fine. If you only have the address I would do
Code: [Select]
$apiParams = array('version' => 3, email = $emailAddress);
$apiEmails = civicrm_api('Email', 'Get', $apiParams);
if ($apiEmails['is_error'] = 0) {
   foreach ($apiEmails as $emailId => $apiEmail) {
      $contactId = $apiEmail['contact_id'];
   }
}
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

sabrina

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.7
  • CMS version: Joomla 2.5.14
  • MySQL version: MySQL 5.5.32
  • PHP version: PHP 5.4.19
Re: debug api Joomla 2.5 PHP
July 29, 2013, 02:11:07 am
Hi

Thanks for all your replies.

I see what you are saying but the
Code: [Select]
$apiEmails = civicrm_api('Email', 'Get', $apiParams); does not return any data and the error is zero, I will try this on the staging site when it is up and running.

I will work with the Getsingle for now and see how this works when adding more data to the test site.

Thank you :)

 

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • debug api Joomla 2.5 PHP

This forum was archived on 2017-11-26.