How To Create an Entity

Entity objects control the data and properties of an entity in LiteCart. It is mostly used for inserting and updating data in MySQL database tables through main operations like create, update, or delete. If you create a new database table, a dedicated entity file can make it easier to work with it.

This is a sample entity file. Change the fields and names to match your needs.

~/includes/entities/ent_generic.inc.php

<?php

class ent_generic {
  public $data;
  public $previous;

  public function __construct($id=null) {

    if (!empty($id)) {
      $this->load($id);
    }
    else {
      $this->reset();
    }
  }

  public function reset() {

    $this->data = [];

    $fields_query = database::query(
      "SHOW FIELDS FROM `". DB_TABLE_PREFIX ."table_name`;"
    );

    while ($field = database::fetch($fields_query)) {
      $this->data[$field['Field']] = database::create_variable($field);
    }

    $this->previous = $this->data;
  }

  public function load($id) {

    if (!preg_match('#(^[0-9]+$|^[0-9a-zA-Z_]$|@)#', $id)) {
      throw new Exception('Invalid entry (ID: '. $id .')');
    }

    $this->reset();

    $generic = database::fetch(database::query("
      SELECT * FROM `". DB_TABLE_PREFIX ."table_name`
      WHERE `id` = ". (int)$id ."
      LIMIT 1;"
    ));

    if ($generic) {
      $this->data = array_replace($this->data, array_intersect_key($generic, $this->data));
    }
    else {
      throw new Exception('Could not find user (ID: '. (int)$id .') in database.');
    }

    $this->previous = $this->data;
  }

  public function save() {

    $conflict_query = database::query("
      SELECT `id` FROM `". DB_TABLE_PREFIX ."table_name`
      WHERE (
        LOWER(`column_1`) = '". database::input(strtolower($this->data['column_data'])) ."'
      )
      ". (!empty($this->data['id']) ? "AND `id` != ". (int)$this->data['id'] : "") ."
      LIMIT 1;"
    );

    if (database::num_rows($conflict_query)) {
      throw new Exception(language::translate('error_generic_conflict', 'There is a conflict in the database'));
    }

    if (empty($this->data['id'])) {
      database::query("
        INSERT INTO `". DB_TABLE_PREFIX ."table_name` (`date_created`)
        VALUES ('". ($this->data['date_created'] = date('Y-m-d H:i:s')) ."');"
      );

      $this->data['id'] = database::insert_id();
    }

    database::query("
      UPDATE `". DB_TABLE_PREFIX ."table_name`
      SET 
        `column_1` = '". database::input(strtolower($this->data['column_1'])) ."',
        `column_2` = '". database::input(strtolower($this->data['column_2'])) ."',
        `column_3` = '". database::input(strtolower($this->data['column_3'])) ."',
        `date_updated` = '". ($this->data['date_updated'] = date('Y-m-d H:i:s')) ."'
      WHERE `id` = ". (int)$this->data['id'] ."
      LIMIT 1;"
    );

    $this->previous = $this->data;

    cache::clear_cache('table_name');
  }

  public function delete() {

    database::query("
      DELETE FROM `". DB_TABLE_PREFIX ."table_name`
      WHERE `id` = ". (int)$this->data['id'] ."
      LIMIT 1;"
    );

    $this->reset();

    cache::clear_cache('table_name');
  }
}

Revisions

Recently Edited Articles
This website uses no cookies and no third party tracking technology. We think we can do better than others and really think about your privacy.