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) »
  • Fatal error when creating a relationship in 4.4.3
Pages: [1]

Author Topic: Fatal error when creating a relationship in 4.4.3  (Read 1091 times)

alainb

  • I’m new here
  • *
  • Posts: 24
  • Karma: 0
  • CiviCRM version: 4.4.x
  • CMS version: Drupal
  • MySQL version: 5.x
  • PHP version: 5.x
Fatal error when creating a relationship in 4.4.3
January 15, 2014, 07:31:30 am
I get this error when I create a relationship using the API in version 4.4.3:

Fatal error: Call to a member function fields() on a non-object in /var/www/sites/all/modules/civicrm/api/v3/utils.php on line 712

Here is test code to simulate the problem:

Code: [Select]
<?php
error_reporting
(E_ALL);
ini_set('display_errors', '1');

// initialize civi
if (!isset($config))
{
    require_once(
$_SERVER['DOCUMENT_ROOT'] . '/sites/default/civicrm.settings.php');
    require_once(
'CRM/Core/Config.php');
 
    
$config = CRM_Core_Config::singleton();
}
require_once 
'api/api.php';

// create relationship
$params = array(
  
"version" => 3,
  
"sequential" => 1,
  
"contact_id_a" => 76, // PLEASE make sure you take the ID of an existing person
  
"contact_id_b" => 77, // PLEASE make sure you take the ID of an existing organization
  
"relationship_type_id" => 5,
  
"is_active" => 1,
);
$result = civicrm_api('relationship', 'create', $params);

echo 
'<p>Done</p>';


Could someone try this in his own 4.4.3 environment?
Do you get a fatal error, too?

Kind regards,
Alain

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Fatal error when creating a relationship in 4.4.3
January 15, 2014, 10:39:10 am
I am able to run that code with no problems in 4.4.
However when I say "that code" i refer to the latter part which calls the api and creates the relationship.
The former part (everything under the code comment "// initialize civi") looks very strange to me. You ought to be able to replace it all with simply:
Code: [Select]
civicrm_initialize();If that fails then probably drupal is not bootstrapped, and so you should do that first.
PS the preferred api wrapper in 4.4 is civicrm_api3() rather than civicrm_api() - it properly throws exceptions and does not require "version" as a param.
Try asking your question on the new CiviCRM help site.

alainb

  • I’m new here
  • *
  • Posts: 24
  • Karma: 0
  • CiviCRM version: 4.4.x
  • CMS version: Drupal
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Fatal error when creating a relationship in 4.4.3
January 16, 2014, 02:26:56 am
Thanks for your reply.

This code is part of an import script and runs in a regular php file. That's why the initialization is a bit awkward.
Just create a php file in the root folder of your drupal + civi website, and copy/paste the code below.

I modified my code to use the civicrm_api3(), as you suggested, and added error handling:

Code: [Select]
<?php
error_reporting
(E_ALL);
ini_set('display_errors', '1');

// initialize civi
if (!isset($config))
{
    require_once(
$_SERVER['DOCUMENT_ROOT'] . '/sites/default/civicrm.settings.php');
    require_once(
'CRM/Core/Config.php');
 
    
$config = CRM_Core_Config::singleton();
}
require_once 
'api/api.php';

// create relationship
$params = array(
  
"version" => 3,
  
"sequential" => 1,
  
"contact_id_a" => 4, // PLEASE make sure you take the ID of an existing person
  
"contact_id_b" => 3, // PLEASE make sure you take the ID of an existing organization
  
"relationship_type_id" => 5,
  
"is_active" => 1,
);

try {
  
$result = civicrm_api3('relationship', 'create', $params);
}
catch (
Exception $e) {
  echo 
"My error handler: " . $e->getMessage();
}


echo 
'<p>Done</p>';

Now, when I use a non-existing person ID, e.g. -4, my error handler is called and I get the message:

My error handler: Invalid Contact ID: There is no contact record with contact_id = -4.

That's good.

But when I use existing contact ID's I get the error

Fatal error: Call to a member function fields() on a non-object in /var/www/sites/all/modules/civicrm/api/v3/utils.php on line 712

and the exception is NOT caught by my code.

Can someone reproduce this in his or her environment?

Thanks
Alain




« Last Edit: January 16, 2014, 02:28:29 am by alainb »

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: Fatal error when creating a relationship in 4.4.3
January 16, 2014, 02:44:37 am
Alain,
is it not easier to use a CiviCRM page in a CiviCRM extension to handle the import? In that case you know your environment is correct? That is the pattern that I usually use.....
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

alainb

  • I’m new here
  • *
  • Posts: 24
  • Karma: 0
  • CiviCRM version: 4.4.x
  • CMS version: Drupal
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Fatal error when creating a relationship in 4.4.3
January 16, 2014, 03:32:09 am
Hello Erik,

I just tried the above code in 4.2.8 and it works.
I upgraded to 4.2.14, deleted the relationship, ran the code again: it works.

So doesn't it work in 4.4?

Alain


Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Fatal error when creating a relationship in 4.4.3
January 16, 2014, 08:35:08 am
Well your code still does not call civicrm_initialize nor does it bootstrap drupal.
As a test, I suggest running it in a properly bootstrapped environment. For example, paste the api call snippet here:
https://github.com/civicrm/civicrm-core/blob/master/CRM/Contact/Page/View/Summary.php#L52
Then visit any contact summary page and the code will run.
Or, even easier, browse to any CiviCRM screen and paste this into your browser's console:
CRM.api('relationship', 'create', {contact_id_a: 76, contact_id_b: 77, relationship_type_id: 5, is_active: 1});
Try asking your question on the new CiviCRM help site.

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: Fatal error when creating a relationship in 4.4.3
January 16, 2014, 08:42:12 am

note that one is a PHP fatal error (i.e. run time error that aborts php) and the other is a civicrm thrown error (invalid contact id)

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

alainb

  • I’m new here
  • *
  • Posts: 24
  • Karma: 0
  • CiviCRM version: 4.4.x
  • CMS version: Drupal
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Fatal error when creating a relationship in 4.4.3
January 17, 2014, 02:48:34 am
Dear all,

I really appreciate your help, but let's not lose the focus on the problem.

I noticed my import script crashes in 4.4.3, while it works perfectly in 4.2.8, 4.2.14, and 4.3.7. I just verified it.
In 4.4.3 it works partially. Things like creating custom fields, enable/disable stuff, creating contacts... all work using the api. But creating relationships produces a fatal error.

That's why, IMHO, I think it's an issue in the api of version 4.4.3.

The code I provided above isolates the problem. You can reproduce it by copy/pasting the code in a file with a .php extension that you put in the root folder or in a sub folder of your drupal + civi environment. It should work in e.g. 4.3.7, and fail in 4.4.3. (I haven't tested it in 4.4.1 and 4.4.2)
It would be great if you could try it.

Thanks a lot.

Alain

PS. My import script imports 40000 contacts, 3500 events, 115000 participants, and 35000 relationships. When I run this in the browser I get an out of memory error. Running it from a bash script works fine, because memory is freed after each import step. By the way, it takes 15 hours to complete!



Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Fatal error when creating a relationship in 4.4.3
January 17, 2014, 09:50:35 am
It would help keep focus on the problem if you would tell me the results of the experiment I asked you to do. That would help narrow down the cause quite a bit.
Try asking your question on the new CiviCRM help site.

alainb

  • I’m new here
  • *
  • Posts: 24
  • Karma: 0
  • CiviCRM version: 4.4.x
  • CMS version: Drupal
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Fatal error when creating a relationship in 4.4.3
January 21, 2014, 07:27:50 am
Hi Coleman,

I did some extensive testing with clean drupal and civi 4.4.x installations, in which it all works (in hooks and standalone). So that's good news.  :)

I found out the fatal error occured with a civi database I have been using for my imports. It's a clean civi database with specific configuration stuff (currency, language, seperator for CSV, ...). The 4.4.3 version of that database, which was upgraded from 4.3.7, seemed to be corrupt for some reason. I'll recreate it manually in 4.4.3, and that will undoubtedly solve the problem.

Thanks.

Kind regards,
Alain

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Fatal error when creating a relationship in 4.4.3
January 21, 2014, 09:04:09 am
Glad you got it figured out.
Try asking your question on the new CiviCRM help site.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Fatal error when creating a relationship in 4.4.3

This forum was archived on 2017-11-26.