You are here

public function EntityType::save in Entity Construction Kit (ECK) 7.2

Same name and namespace in other branches
  1. 7.3 eck.classes.inc \EntityType::save()

Save.

Overrides DBObject::save

File

./eck.classes.inc, line 384
Classes for all the different objects used in ECK.

Class

EntityType
An entity type database object.

Code

public function save() {
  if ($this->isNew) {
    module_load_include('inc', 'eck', 'eck.entity_type');
    $schema = eck__entity_type__schema($this);
    db_create_table("eck_{$this->name}", $schema);

    // Allow other modules to respond to the creation of entity types.
    module_invoke_all('eck_entity_type_insert', $this);
  }
  else {

    // Modify the already existing table in accordance with the
    // recorded changes.
    if (array_key_exists('add', $this->changes)) {
      foreach ($this->changes['add'] as $name) {

        // First lets get the record.
        $properties = $this->properties;
        $property = $properties[$name];

        // Now we check to see whether it is a default or a custom property
        // it is not custom so lets get the schema and add the field.
        $schema = eck_property_type_schema($property['type']);
        db_add_field("eck_{$this->name}", $name, $schema);
      }
    }
    if (array_key_exists('remove', $this->changes)) {
      foreach ($this->changes['remove'] as $name) {
        db_drop_field("eck_{$this->name}", $name);
      }
    }

    // Allow other modules to respond to the change of entity types.
    module_invoke_all('eck_entity_type_update', $this);
  }
  parent::save();
  global $_eck_entity_types_cache;
  $cache_enabled = isset($_eck_entity_types_cache) ? TRUE : FALSE;
  if ($cache_enabled) {
    $_eck_entity_types_cache
      ->reset();
  }
  eck_clean_up_request();
}