Subversion Repositories PHPX

Rev

Rev 29 | Rev 35 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
<?php

require_once __DIR__ . '/../Application.php';
require_once __DIR__ . '/../AbstractModel.php';

/**
 * Generic database table model class
 *
 * @author Thomas Lahn
 * @property-read int $lastInsertId
 *   ID of the last inserted row, or the last value from
     a sequence object, depending on the underlying driver.
 */

class Table extends AbstractModel
{
  /**
   * Name of the table
   */

  protected $_name = '';
 
  /**
   * Database of the table
   * @var Database
   */

  protected $_database;
 
  protected $_id = 'id';
 
  /**
   * Creates a new <code>Table</code> instance.
   *
   * Each of the parameters is optional and can also be given
   * by a protected property where the parameter name is preceded
   * by <code>_</code>.  Parameter values overwrite the default
   * property values.  It is recommended to use default property
   * values of inheriting classes except for small applications
   * and testing purposes.
   *
   * @param Database $database
   *   Database of the table (required in order to use a fitting
   *   query language)
   * @param string $name
   *   Table name
   * @param string $id
   *   Name of the primary key column
   * @throws InvalidArgumentException
   */

  public function __construct(Database $database = null, $name = '', $id = '')
  {
    if ($database === null)
    {
      $this->_database = Application::getInstance()->getDefaultDatabase();
    }
    else
    {
      $this->_database = $database;
    }
   
    if ($name !== '')
    {
      $this->_name = $name;
    }
   
    if (!$this->_name)
    {
      throw new InvalidArgumentException('a table name is required');
    }

    if ($id !== '')
    {
      $this->_id = $id;
    }
  }
 
  /**
   * Returns the database for the table
   * @return Database
   */

  public function getDatabase()
  {
    return $this->_database;
  }
 
  /**
   * Initiates a transaction
   *
   * @return bool
   * @see Database::beginTransaction()
   */

  public function beginTransaction()
  {
    return $this->_database->beginTransaction();
  }
 
  /**
   * Rolls back a transaction
   *
   * @return bool
   * @see Database::rollBack()
   */

  public function rollBack()
  {
    return $this->_database->rollBack();
  }
 
  /**
   * Commits a transaction
   *
   * @return bool
   * @see Database::commit()
   */

  public function commit()
  {
    return $this->_database->commit();
  }
 
  /**
   * Retrieves all rows from the table
   *
   * @return array
   * @see Database::fetchAll()
   */

  public function fetchAll($fetch_style = null, $column_index = null, array $ctor_args = null)
  {
    return $this->_database->fetchAll($this->_name, $fetch_style, $column_index, $ctor_args);
  }
 
  /**
   * Selects data from one or more tables
   *
   * @return array
   * @see Database::select()
   */

  public function select($columns = null, $where = null, $order = null, $limit = null)
  {
    return $this->_database->select($this->_name, $columns, $where, $order, $limit);
  }
 
  /**
   * Updates records in one or more tables
   *
   * @return bool
   * @see Database::update()
   */

  public function update($data, $condition)
  {
    return $this->_database->update($this->_name, $data, $condition);
  }
 
  /**
   * Inserts a record into the table
   *
   * @return bool
   * @see Database::insert()
   */

  public function insert($data, $cols = null)
  {
    return $this->_database->insert($this->_name, $data, $cols);
  }
 
  /**
   * Returns the ID of the last inserted row, or the last value from
   * a sequence object, depending on the underlying driver.
   *
   * @return int
   * @see Database::getLastInsertId()
   */

  public function getLastInsertId()
  {
    return $this->_database->lastInsertId;
  }
 
  /**
   * Delete a record from the table
   *
   * @param int $id
   *   ID of the record to delete.  May be <code>null</code>,
   *   in which case <var>$condition</var> must specify
   *   the records to be deleted.
   * @param array[optional] $condition
   *   Conditions that must be met for a record to be deleted.
   *   Ignored if <var>$id</var> is not <code>null</code>.
   * @return bool
   * @throws InvalidArgumentException if both <var>$id</var> and
   *     <var>$condition</var> are <code>null</code>.
   * @see Database::delete()
   */

  public function delete($id, array $condition = null)
  {
    if (!is_null($id))
    {
      $condition = array($this->_id => $id);
    }
    else if (is_null($condition))
    {
      throw new InvalidArgumentException(
        '$id and $condition cannot both be null');
    }
   
    return $this->_database->delete($this->_name, $condition);
  }
 
 /**
  * Inserts a row into the table or updates an existing one
  *
  * @param array $data
  *   Associative array of column-value pairs to be updated/inserted
  * @param string|array $condition
  *   If there are no records matching this condition, a row will be inserted;
  *   otherwise matching records are updated
  * @return bool
  * @see Table::update()
  * @see Table::insert()
  */

  public function updateOrInsert($data, array $condition = null)
  {
    if ($this->select($this->_id, $condition))
    {
      return $this->update($data, $condition);
    }

    return $this->insert($data);
  }

  /**
   * Finds a record by ID
   *
   * @param mixed $id
   */

  public function find($id)
  {
    /* DEBUG */
    if (defined('DEBUG') && DEBUG > 0)
    {
      debug($id);
    }
   
    return $this->select(null, array($this->_id => $id));
  }
}