You are here

abstract class Messaging_Object in Messaging 6.4

Same name and namespace in other branches
  1. 7 messaging.module \Messaging_Object

Messaging persistent object

This will be the base class for all objects that:

  • Are persistent (db storage)
  • Have a user account associated and language (Messaging_User_Object)

Hierarchy

Expanded class hierarchy of Messaging_Object

File

includes/messaging_object.class.inc, line 144
Drupal Messaging Framework - Base classes

View source
abstract class Messaging_Object extends Messaging_User_Object {

  // Database properties
  const DB_TABLE = '';
  const DB_KEY = '';

  // Deletion mark. Deleted objects will keep their id, may be needed to remove related data, but will be marked
  protected $deleted = FALSE;

  /**
   * Constructor, with predefined array of data
   */
  public function __construct($template = NULL) {
    if ($template) {
      foreach ($template as $key => $value) {
        $this->{$key} = $value;
      }
    }

    // If loaded from db, may need unserialize
    if ($this
      ->key()) {
      $this
        ->unserialize();
    }

    // Make sure all objects have language
    if (!isset($this->language)) {
      $this
        ->set_language();
    }
  }

  /**
   * Build object from template
   */
  public static function object_build($template, $class) {
    if (is_object($template) && is_a($template, $class)) {
      return $template;
    }
    else {

      // This seems to be the only way of invoking the build method of the class
      return call_user_func(array(
        $class,
        'build',
      ), $template);
    }
  }

  /**
   * Get unike id key
   */
  function key() {
    return isset($this->{$this
      ->db_key()}) ? $this->{$this
      ->db_key()} : NULL;
  }

  /**
   * Save to database
   */
  public function save() {
    if ($this
      ->key()) {
      return $this
        ->update();
    }
    else {
      return $this
        ->insert();
    }
  }

  /**
   * Create object in database
   */
  public function insert() {
    return drupal_write_record($this
      ->db_table(), $this);
  }

  /**
   * Update object in database
   */
  public function update() {
    return drupal_write_record($this
      ->db_table(), $this, $this
      ->db_key());
  }

  /**
   * Delete from system database
   */
  public function delete() {
    if ($this
      ->key()) {
      db_query('DELETE FROM {' . $this
        ->db_table() . '} WHERE ' . $this
        ->db_key() . ' = %d', $this
        ->key());

      //unset($this->{$this->db_key()});
      $this->deleted = TRUE;
    }
  }

  /**
   * Check whether this object is an instance or just a template
   */
  public function is_instance() {
    return !$this->deleted && $this
      ->key();
  }

  /**
   * Load object by unique key, no static caching
   */
  public static function object_load($table, $key, $value, $class = NULL) {
    if ($object = self::db_load($table, $key, $value)) {
      return $class ? self::object_build($object, $class) : $object;
    }
  }

  /**
   * Unserialize after loading. It does nothing but can be overridden
   */
  public function unserialize() {
  }

  /**
   * Load object from DB
   */
  public static function db_load($table, $key, $value) {
    return db_fetch_object(db_query("SELECT * FROM {" . $table . "} WHERE {$key} = %d", $value));
  }

  /**
   * Magic function. Set protected properties
   *
   * We have a problem when creating records. Serial fields are unset() by drupal_write_record
   * For these fields, though they're public, when there's a __set function it is called too
   */
  public function __set($name, $value) {
    if ($name == $this
      ->db_key()) {
      $this->{$name} = $value;
    }
    else {
      parent::__set($name, $value);
    }
  }

  // Get table name for storage
  public static abstract function db_table();

  // Get key field name for storage
  public static abstract function db_key();

  // Load from key field
  public static abstract function load($key);

  // Build object form template
  public static abstract function build($template);

}

Members

Namesort descending Modifiers Type Description Overrides
Messaging_Object::$deleted protected property
Messaging_Object::build abstract public static function 1
Messaging_Object::db_key abstract public static function
Messaging_Object::DB_KEY constant 2
Messaging_Object::db_load public static function Load object from DB
Messaging_Object::db_table abstract public static function
Messaging_Object::DB_TABLE constant 2
Messaging_Object::delete public function Delete from system database 1
Messaging_Object::insert public function Create object in database 1
Messaging_Object::is_instance public function Check whether this object is an instance or just a template
Messaging_Object::key function Get unike id key
Messaging_Object::load abstract public static function 1
Messaging_Object::object_build public static function Build object from template
Messaging_Object::object_load public static function Load object by unique key, no static caching 1
Messaging_Object::save public function Save to database
Messaging_Object::unserialize public function Unserialize after loading. It does nothing but can be overridden
Messaging_Object::update public function Update object in database
Messaging_Object::__construct public function Constructor, with predefined array of data 1
Messaging_Object::__set public function Magic function. Set protected properties