Skip Navigation

The Entity Class

The entity class abstracts the process of getting information from Reason entities. The entity class also attempts to make code as efficient as possible, by minimizing the number of queries to the database through caching.

Information Retrieval Methods

Below are basic descriptions of core entity class methods:

  • id() – returns the id of the given entity. Uses no database queries.
  • get_values() – returns an associative array of all database values of the given entity. The name of the field is the index. For each value.
  • get_value( $col ) – returns a single value of the entity, where $col is the name of a field. returns false if $col isn't a field of the given entity.
  • get_characteristics() – returns and array of the fields of a given entity.
  • get_display_name() – if the type has a custom display name handler, uses that to return a name. Otherwise, returns the name of the entity.

Relationship Information Retrieval Methods

These are best avoided as they are not very efficient. Use entity selectors to grab entities along with relevant information from related entities.

  • has_right_relation_of_type( $e ) – returns true if the entity has a right relationship of type $e, where $e is a name or id from the allowable_relationship table.
  • has_left_relation_of_type( $e ) – returns true if the entity has a left relationship of type $e, where $e is a name or id from the allowable_relationship table.
  • has_relation_of_type( $e ) – returns true if the entity has a right or left relationship of type $e, where $e is a name or id from the allowable_relationship table.
  • has_right_relation_with_entity( $e , $type = false ) – if type is specified, returns true if the given entity has a right relationship of type $type with entity $e. If type is not specified, returns true if the given entity has any type of right relationship with $e.
  • has_left_relation_with_entity( $e , $type = false ) – if type is specified, returns true if the given entity has a left relationship of type $type with entity $e. If type is not specified, returns true if the given entity has any type of left relationship with $e.
  • has_relation_with_entity( $e , $type = false ) – if type is specified, returns true if the given entity has a right or left relationship of type $type with entity $e. If type is not specified, returns true if the given entity has any type of right or left relationship with $e.
  • get_right_relationship( $rel_name ) – returns an array of all entities with which the given entity has a right relationship of type $rel_name, where $rel_name is an id or name from the allowable_relationship table.
    • Note: This gets entities where the current entity is on the right (B) side.
  • get_left_relationship( $rel_name ) – returns an array of all entities with which the given entity has a left relationship of type $rel_name, where $rel_name is an id or name from the allowable_relationship table.
    • Note: This gets entities where the current entity is on the left (A) side.
  • get_relationship( $rel_name ) – returns an array of all entities with which the given entity has a right or left relationship of type $rel_name, where $rel_name is an id or name from the allowable_relationship table.
  • get_right_relationships() – returns an array of arrays which the first index is the name or id of a given allowable right relationship of the type of the given entity, and the second is an array of entities.
  • get_left_relationships() – returns an array of arrays which the first index is the name or id of a given allowable left relationship of the type of the given entity, and the second is an array of entities.
  • get_right_relationships() – returns an array of arrays which the first index is the name or id of a given all allowable relationship of the type of the given entity, and the second is an array of entities.

Tips and Tricks

  1. Use the id() method instead of get_value('id') to avoid unnecessary database queries. The id of an instantiated entity is always available without a query - any use of get_value could result in a query if get_value was not previously called for the entity.

  2. The entity class has a set_value method that can be used to attach "virtual" properties to an entity. This concept can be powerful and convenient way to augment the information carried by an entity.