Relationships

Entity relationships are the part of the Entity API that represent connections between different entities. For example, a post entity is related to the user entity that is the author of the post.

Each relationship that a type of entity has is represented by a class. Often the which entity(ies) an entity relates to is stored in a field one of the entity objects in the database. So let’s use the post author relationship as an example, since the ID of the user who is the author of a post is stored on the post_author field of the WP_Post object.

As you can see, we extend the WordPoints_Entity_Relationship_Stored_Field class, since the ID of the post author is stored on an entity field as pointed out above. In other cases we might need to extend WordPoints_Entity_Relationship directly, but here we can just let this class to most of the heavy lifting for us. So all we have to do is define a few properties and a simple method:

  • $storage_type — How this entity relationship is stored. In this case, it is stored in the database, so the value of this field is 'db'.
  • $primary_entity_slug — The slug of the primary entity in the relationship. This in this case, it is the 'post'.
  • $related_entity_slug — The slug of the related entity. In the case of the post author relationship, the post author is a 'user'. For one-to-many relationships, we would append {} to the slug of the related entity to indicate that it is an array, like this: 'user{}'. But in this case, each post has just one author.
  • $related_ids_field — The field on the primary entity object where the ID(s) of the related entity(ies) are stored. The ID of the user that is the author of a post is stored in the post_author field for that post.
  • get_title() — This method should return the title of the relationship. Note that you shouldn’t include the name of the primary arg here: use 'Author', not 'Post Author'.

If your entity relationship isn’t stored on a field of the primary entity, you would need to do something like this instead:

You can see that we have to define two additional methods:

  • get_related_entity_ids() — This method is passed the ID of the primary entity in a relationship, and should return the ID(s) of the related entity(ies). In the example above, the roles of a user actually are provided on the WP_User object via the roles property, even though they aren’t stored in a field on the user roles table.
  • get_storage_info() — This method needs to return information about how this entity is stored. In the example above, this is pretty complex, although it will usually be simpler than that. For more information about how to provide storage info for a relationship, check out the relationship storage info docs.

Once you’ve created your entity relationship class, you just need to register your class so that WordPoints knows that it exists:

As you can see, we hook into the initialization of the entities registry, just as we do when registering a new entity. So you can register all of your entities and entity children in a single function. We just get the entity children class registry, which is a sub-app of the entities app, and register our relationship with the entity slug and a child slug that is unique for that entity.