could you help? Error while creating a relation

Events happening in the community are now at Drupal community events on www.drupal.org.
avuryoni@gmail.com's picture

Hey,

I'm building a website based on Campaignion (and Redhen), and I'm trying to create a relation when a contact is being created.

for now - just trying to create a relation doesn't work. could you please help? I'm using hook_init just for test purposes, while trying to create a relation.

relation_save is the line which causes the error which I can't figure out:

Fatal error: Class name must be a valid object or a string in /var/www/html/cpn3/includes/common.inc on line 7999

Here's the code.

Thanks a million:

// the name of my module is campsite
// hook init is used here just for testing purposes
function campsite_init($page)
{

$relation_bundle = 'redhen_affiliation';

$endpoints = array();
$endpoints[] = array('entity_type' => 'redhen_contact', 'entity_id' => '48');
$endpoints[] = array('entity_type' => 'campsite_org', 'entity_id' => '10');

$new_relation = relation_create($relation_bundle, $endpoints, NULL);

$new_relation->redhen_relation_roles= array();
$new_relation->redhen_relation_roles['und'] = array();
$new_relation->redhen_relation_roles['und'][0]= array( 'value' => '1');

$new_relation->redhen_relation_status = array();
$new_relation->redhen_relation_status['und'] = array();
$new_relation->redhen_relation_status['und'][0]= array('value' => 1);

$new_relation->relation_type = 'redhen_affiliation';

relation_save($new_relation);

}

Comments

You should be using metadata

gcb's picture

You should be using metadata wrappers to set these values. Any time you are manipulating an entity and find yourself using ['und'], you should stop and create a wrapper. Also, you appear to be using an entity type of "campsite_org". Redhen Affiliations are for connecting redhen_contacts to redhen_orgs.

$relation = relation_create('redhen_affiliation', array(
  array(
    'entity_type' => 'redhen_contact',
    'entity_id' => 48,
  ),
  array(
    'entity_type' => 'redhen_org',
    'entity_id' => 10),
));
$wrapper = entity_metadata_wrapper('relation', $relation);
$wrapper->redhen_relation_status = 1;
relation_save($relation);

Or something like that.

RedHen CRM

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: