How can I get referencing entity Id from referenced entity? - drupal-7

I have 3 content types A, B, C. B and C have entity reference fields that reference to A and B and with different machine names. I want to get the parent node ID from referenced node.
A is referenced to B
B is referenced to C
Now if I am on A then I want to get B node id
and if I am on B then I want to get C node id.
How can I achieve this programatically.

You could do this with the Corresponding Entity References module, which can create a two-way reference when a single reference is set. That way it prevents having to search for the parent.
Otherwise, you can use Views, which can search for the parent via Relationships.

Related

Genealogy: Relating family relationship in database

How is the algorithm for family tree in genealogy be done?
For example Parent A has a Children B and C. So, what if Children C produce a Children too in the future. How does it add to the tree by using database?
I've looked on Jit's RGraph tree graph where it populates the tree by using JSON data. I might wanna expand the tree by populating the JSON from a database. But, I couldn't figure out how will the database look like if I add more children to the data. I think it is doable if it was only for JSON. But I can't fully grasp when it comes to dynamic data.
What model/structure is suitable to solve this kind of problem?
It could be as simple as this:
FK1 and FK2 that denote FOREIGN KEYs (FATHER_ID references PERSON_ID and MOTHER_ID also references PERSON_ID). FATHER_ID and/or MOTHER_ID can be left NULL if unknown.
This model is not perfect. For example, it doesn't enforce parents' gender (the father is male and mother female) and it only represents biological parents, but not other kinds of relationships such as adoption (all of which can be done, but with certain complications in the model).
However it's very simple and can be naturally traversed (in recursive fashion), analyzed or exported.
For example Parent A has a Children B and C. So, what if Children C produce a Children too in the future. How does it add to the tree by using database?
A child D of C could be represented like this:
PERSON_ID FATHER_ID MOTHER_ID
A
B A
C A
D C
Or like this if C is a mother:
PERSON_ID FATHER_ID MOTHER_ID
A
B A
C A
D C
IMO it's an adjacency list model with some extension. For example to have 2 parents you can simply add a special (end) node or you can mark the node with a comment.
Might look at prior post using GEDCOM model
I use Union and Person tables with a common Union_ID key in both. In the Person table, this refers to the parents. In the graph, the Union may have 2 inbound parents and an unlimited number of outbound children.

What will be the Correct Defination of Junction Object in Salesforce

Recently I was puzzled with the Junction Object, well I am clear about this thing, But I require a proper definition with an example, So I can understand it more easily and clear manner.
My actual problem is here:--
The junction Object provides a many to many relationship, So surely in this relationship, one will be acted as master and other one as child relationship or vice verse. So Now As you also know that when we delete a master entry, then the resultant child entreies will also be removed/deleted which are attached to that Master entry. In our case we have a two sided Master-child relationship. means a Master is a child for an another object and vice verse, So If we deleted an entry in any of the side then it will be deleted/removed other entries also, and as the other other entries removed which are also attach to the previously removed entry side then both side of entries will removed, So I/m just confused in it, that how's our junction Object functionality is working.
Its Many-to-many relationship.
For example you have object Position and object Candidate.
Each candidate can be related to many jobs and each Position can be related to many candidates.
For this purpose create a custom object "junction object" with two master-detail relationship fields, linking to the object Job and object Position which represented this kind of relationship.

trying to sort on a reference property of an entity but don't want to sort on object but a ppty of the object

Say I have done a query and I want to then order it by a reference property attribute - can I temporarily add a field in the results of my query and add in the attribute to sort it before I send it to the template?
q = LetterTable.all().order('votes)
but then I want to take the q results above and sort on a property of LetterTable called "Person". However, 'person' is a reference to an instance on the PersonTable, so I don't want the reference object, I want to sort by person.name. How can I do this?
Anyone?
You can't, at least not stright out of the box. There's some page in the GAE docs that describe how queries work. Essentially your entities are indexed by property, and when you run a query, it just looks through the index.
In your case, the index contains the reference property and not the PersonTable entity it refers to, so the index doesn't have person.name.
The appropriate way to do this in App Engine is to denormalize. That means storing an extra copy of some data you need, in this case, you'd store a copy of person.name inside your LetterTable entity. Then you can query and sort on that name.
class LetterTable(db.Model):
person = db.ReferenceProperty(Person)
person_name = db.StringProperty()
This method certainly has its drawbacks, the primary being keeping data in sync (ie, if a person changes their name, you'll have to find all LetterTable instances that refer to that person, and change all the denormalized names).

Creating a data object relationship system

I would like to store an object that has a name and some attribute and any kind of database and have another that that document relationship between between them
Let say this
Object A
Object B
Object C
Object D
A is related to B
A is related to D
B is related to C
All relation have also attribute that give the relation order (uni (from,to) or (to,from) or bidrectional).
Then, I would like to do some query that will show me A is related to C by passing in B. A kind of path between 2 objects.
Kind of graph database like Neo4J
Thanks for your input
Question is rather old but anyways:
The Neo4j documentation actually has an example that is very close to answering the question.
http://docs.neo4j.org/chunked/stable/graph-algo-path-finding.html

What is the effect of deleting an entity's ancestor on its path?

If I have entities a, b and c such that a is the parent of b and b is the parent of c, will deleting b have any effect on the path of c?
No. It's quite possible for an entity to have a parent that no longer exists - the entity, and the identity of its entity group, will remain unchanged.

Resources