Closed (fixed)
Project:
Relation
Version:
7.x-1.x-dev
Component:
API
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
7 Jan 2011 at 07:36 UTC
Updated:
28 Jan 2016 at 05:52 UTC
Jump to comment: Most recent
Comments
Comment #1
naught101 commenteder.. what errors?
Comment #2
aidanlis commentedThe 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".
Comment #3
naught101 commentedCan 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.
Comment #4
aidanlis commentedIt'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.
Comment #5
jax commentedsubscribe
This seems to work if you want to add a relation field to your entity/content type:
But I don't think that's what you're trying to achieve...
Comment #6
aidanlis commentedThanks 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.
Comment #7
dpiTry this:
Comment #8
aidanlis commentedIt looks like the best way to do this would be with the entity_metadata_wrapper().
Comment #10
naught101 commentedFixed in reboot branch.
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.
Comment #11
Letharion commentedOn 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?
Comment #12
Letharion commentedIf I look at the right branch, it's so much easier to find the function.
Comment #14
Letharion commentedReopening, again, because this seems to have changed significantly recently.
Comment #15
chx commentedA little, now you pass a list of associative arrays to relation_create, each having an entity_type and an entity_id.
Comment #17
velebak commentedThe 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.
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
Comment #18
chx commentedinsertion_order is a bundle (node type) and so is preprint_insert. You wanted entity types and not bundles in there.
array('node', $nid)Comment #19
naught101 commentedThe 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.
Comment #20
mikran commentedOkay, here is updated code #10 and #17 examples combined.
Comment #21
velebak commentedThanks @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:
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?
Comment #22
naught101 commentedrelation_insert is fixed, and now returns the $rid
have you tried
relation_load($rid)after creating the above relations? what does that return?Comment #23
velebak commented@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!
Comment #24
velebak commentedOkay, 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.
Comment #25
mikran commentedRight, I should have noticed that from the space in bundle type name but I'm glad you figured it out eventually.
Comment #27
webankit commentedhow to update relation fields programatically?
Comment #28
mikran commentedThat is same as any entity basically.
Comment #29
dpiPlease create new issues in the future.
Comment #30
javed kureshi commentedI want to create relation with relation module by using custom module .. how i create custom module please help me,