Subversion Repositories PHPX

Rev

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

1
<?php

namespace PointedEars\PHPX\Db;

use \PointedEars\PHPX\Application;

/**
 * Generic database table model class
 *
 * @author Thomas Lahn
 * @property Database $database
 * @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 \PointedEars\PHPX\AbstractModel
{
  /**
   * Name of the table
   */

  protected $_name = '';

  /**
   * Database of the table
   * @var Database|string
   */

  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 = null, $name = '', $id = '')
  {
    if ($database === null)
    {
                /* Call getter to convert to Database if possible */
        if ($this->database === null)
        {
                $this->_database = Application::getInstance()->getDefaultDatabase();
        }
    }
    else
    {
      $this->_database = $database;
    }

    if ($name !== '')
    {
      $this->_name = $name;
    }

    if (!\is_string($this->_name))
    {
      throw new \InvalidArgumentException(
        'Expected string for table name, saw '
                                . \get_class($this->_name) || \gettype($this->_name));
    }

    if ($id !== '')
    {
      $this->_id = $id;
    }
  }

  /**
   * Returns the database for the table
   * @return Database
   */

  public function getDatabase()
  {
    if (\is_string($this->_database))
    {
      /* Call setter to convert to Database */
      $this->database = $this->_database;
    }

    return $this->_database;
  }

  /**
   * @param Database|string $value
   * @throws InvalidArgumentException
   */

  public function setDatabase ($value)
  {
        if ($value instanceof Database)
        {
                $this->_database = $value;
        }
        else if ($value !== null)
        {
                $database = new $value();
                if (!($database instanceof Database))
                {
                        throw new \InvalidArgumentException(
                                'Expected Database instance or string for class name, saw '
                                        . (\get_class($value) || \gettype($value))
                        );
                }

                $this->_database = $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 ($id !== null)
    {
      $condition = array($this->_id => $id);
    }
    else if ($condition === null)
    {
      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
   * @return array
   */

  public function find ($id)
  {
    /* DEBUG */
    if (defined('DEBUG') && DEBUG > 0)
    {
      debug($id);
    }

    $result = $this->select(null, array($this->_id => $id));

    if ($result)
    {
      $result = $result[0];
    }

    return $result;
  }
}