I tried something like:

$node->field_authors[LANGUAGE_NONE][0]['value'] = array('other_entity_type'=>'node', 'other_entity_id'=>$node->nid);

But got angry errors about array_flip.

Comments

naught101’s picture

Status: Active » Postponed (maintainer needs more info)

er.. what errors?

aidanlis’s picture

The same errors as these 122 bug reports: http://drupal.org/search/apachesolr_multisitesearch/array_flip

I'm guessing it's something to do with the entity system ...

I guess this was more of a "how should it be done" rather than "it's broken".

naught101’s picture

Can you please post a sample of your error messages, and steps to reproduce?

If you think this isn't a bug with relation, but with entity.module, please move it there.

aidanlis’s picture

It's a support request, not a bug report - I'm asking how it's meant to be done, because the above way gives array_flip errors.

jax’s picture

subscribe

This seems to work if you want to add a relation field to your entity/content type:

$field = array(
      'field_name' => 'department_firm_reference',
      'cardinality' => 1,
      'type'        => 'relation',
);
field_create_field($field);
$instance = array(
  'field_name' => 'department_firm_reference',
  'label' => 'Firm',
  'widget' => array(
    'type' => 'relation_default',
  ),
),
$instance['entity_type'] = 'node';
$instance['bundle'] = 'department';
field_create_instance($instance);

But I don't think that's what you're trying to achieve...

aidanlis’s picture

Thanks for documenting that Jax, I'm sure it'll come in handy. I've already got the field set up, my post is about adding relations to the node programatically.

dpi’s picture

Try this:

$node_id_left = 1;
$entity_type_right = 'node';
$entity_id_right = 2;
$relation_field = 'relation_field_name_here';

$node = node_load($node_id_left);
$node->{$relation_field}[LANGUAGE_NONE][] = array(
  'other_entity_type' => $entity_type_right,
  'other_entity_id' => $entity_id_right,
);
node_save($node);
aidanlis’s picture

It looks like the best way to do this would be with the entity_metadata_wrapper().

naught101’s picture

Status: Postponed (maintainer needs more info) » Fixed

Fixed in reboot branch.

$predicate = 'likes';
$entity_keys = array(
  array('user', 6),
  array('node', 4),
);
relation_create($predicate, $entity_keys);

Will create a relation between user 6 and node 4, of the type 'likes'. At the moment, you need to pre-create the relation type ($predicate) in admin/structure/relation.

Letharion’s picture

Status: Fixed » Active

On the same topic, I'm trying to write the code to create relations from Rules.
I'm looking in the code for this "relation_create" function, but I can't seem to find it.

Which function would I direct Rules to call when creating a new Relation?

Letharion’s picture

Status: Active » Fixed

If I look at the right branch, it's so much easier to find the function.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Letharion’s picture

Status: Closed (fixed) » Active

Reopening, again, because this seems to have changed significantly recently.

chx’s picture

Status: Active » Fixed

A little, now you pass a list of associative arrays to relation_create, each having an entity_type and an entity_id.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

velebak’s picture

Status: Closed (fixed) » Active

The code in post #10 doesn't actually save the relation, you have to call relation_save with the return value from relation_create. relation_insert is coded to do both with one call.

However, I'm having issues with relations not getting created at all. I see versions in the relation and relation_revision tables, but nothing in field_data_endpoints or field_revision_endpoints.

        $relmap = array();  // to save the relations
 
//node creation happens here.  works fine.  nodes are saved to system without incident.
       //$order_id = nid of an insertion_order node 
       //$preprint_nid = nid of a preprint_insert node

//in this use case, I have multiple order nodes being related to a single preprint insert node.

        $relmap[]=array(
                array('insertion_order',$order_nid),
                 array('preprint_insert',$preprint_nid)
                        );
 
        //relate the insertion order nodes to one preprint insert node
        $predicate = 'belongs to';  //already created as relation type
        //create the relations to the preprint insert node
        foreach($relmap as $relation){
                relation_insert($predicate,$relation);
        }

Ideas? I don't get any errors. However, when I try to put together a view showing the data, there's no related orders on the common preprint insert node. There's also nothing listed at admin/content/relation

chx’s picture

Status: Active » Closed (fixed)

insertion_order is a bundle (node type) and so is preprint_insert. You wanted entity types and not bundles in there. array('node', $nid)

naught101’s picture

Status: Closed (fixed) » Active

The reason the code in #10 is wrong is that it's 9 months old. A LOT has changed since then. But that code will still work if you replace _create with _insert.

You don't need that foreach. You need to put your $relmap in directly as the endpoint. See the doxygen for relation_create(). http://drupalcode.org/project/relation.git/blob/refs/heads/7.x-1.x:/rela...

Looks like relation_insert() needs to return relation_save, not just run it. Also, doxygen needs to be updated for those api functions.

mikran’s picture

Okay, here is updated code #10 and #17 examples combined.

$endpoints = array(
  array('entity_type' => 'node', 'entity_id' => $order_nid),
  array('entity_type' => 'node', 'entity_id' => $preprint_nid)
);
$rid = relation_insert('belongs to', $endpoints);
velebak’s picture

Thanks @chx, @naught101 and @mikran. I appreciate all your help. However, I gave everyone's suggestions a try with no success thus far.

@naught101, there's a snippet of code I failed include in my example with regards to $relmap. It contains a list of all the relations I'm going to create. I may have 1+ insertion_order nodes that must be related to a single preprint_insert node. Hence the array/list. Thanks for catching that however.

@mikran, relation_insert does not return anything. I just broke my code up into the constituent relation_create and relation_save calls to get a $rid.

I'm using:

D7.10
Relation 7.x-1.x-dev (indicated as 7.x-1.0-beta3+39-dev in the Modules screen)

Chunk of example code:


//@chx's suggested relation array contents, tried this way first	    
		$endpoints=array(
			array('node', $order_nid),
			array('node', $preprint_nid)
		);
//@mikran's suggested relation array contents, tried this way second.
		$endpoints = array(
  			array('entity_type' => 'node', 'entity_id' => $order_nid),
  			array('entity_type' => 'node', 'entity_id' => $preprint_nid)
		);
		
		//creating the relation and saving it.		
		$relation = relation_create('belongs to',$endpoints);
		$rid = relation_save($relation);
		

No errors are thrown using this code with either suggested array configuration.

I notice no relations are created as no corresponding entries exist in the field_data_endpoints table. $rid does contain a return value that corresponds to an entry in the relation table. relation_revision is also updated, but on my system all the rid fields are 0.

If I use the Relation module's entity collector block, I can create a relation, successfully. Entries appear in the field_data_endpoints table properly. relation_revision is similarly updated, but all the rid fields are still 0.

Is there something else I am doing incorrectly here?

naught101’s picture

relation_insert is fixed, and now returns the $rid

have you tried relation_load($rid) after creating the above relations? what does that return?

velebak’s picture

@naught101, I added code to immediate load the relation after I saved it as you suggested. Called dpm() on the relation, and it shows the proper data.

Not sure what changed as I walked away from this for some time today, but things seem to be working as of right now.

Turned off the relation_load() and dpm() and things seem to still be working.

@naught101, thanks for the assistance. I'm officially shaking my head!

velebak’s picture

Okay, I think I figured out why my relations weren't being created.

The predicate name passed to relation_create must be a machine name version, not the plain text version.

In my case, my relation bundle type was called 'belongs to'. The relations would not be created properly unless I used 'belongs_to' as the predicate name.

mikran’s picture

Status: Active » Fixed

Right, I should have noticed that from the space in bundle type name but I'm glad you figured it out eventually.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

webankit’s picture

Component: Code » API
Status: Closed (fixed) » Active

how to update relation fields programatically?

mikran’s picture

That is same as any entity basically.

$relation->field_name[LANGUAGE_NONE][0]['value'] = 'new_value';
relation_save($relation);
dpi’s picture

Status: Active » Closed (fixed)

Please create new issues in the future.

javed kureshi’s picture

Issue summary: View changes

I want to create relation with relation module by using custom module .. how i create custom module please help me,