Subversion Repositories PHPX

Compare Revisions

Last modification

Ignore whitespace Rev 49 → Rev 50

/trunk/Db/Table.php
7,6 → 7,7
* 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.
17,15 → 18,15
* Name of the table
*/
protected $_name = '';
 
/**
* Database of the table
* @var Database
* @var Database|string
*/
protected $_database;
 
protected $_id = 'id';
 
/**
* Creates a new <code>Table</code> instance.
*
45,25 → 46,31
* Name of the primary key column
* @throws InvalidArgumentException
*/
public function __construct(Database $database = null, $name = '', $id = '')
public function __construct($database = null, $name = '', $id = '')
{
if ($database === null)
{
$this->_database = Application::getInstance()->getDefaultDatabase();
/* 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 (!$this->_name)
 
if (!\is_string($this->_name))
{
throw new InvalidArgumentException('a table name is required');
throw new \InvalidArgumentException(
'Expected string for table name, saw '
. \get_class($this->_name) || \gettype($this->_name));
}
 
if ($id !== '')
71,7 → 78,7
$this->_id = $id;
}
}
 
/**
* Returns the database for the table
* @return Database
78,10 → 85,41
*/
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
91,7 → 129,7
{
return $this->_database->beginTransaction();
}
 
/**
* Rolls back a transaction
*
102,7 → 140,7
{
return $this->_database->rollBack();
}
 
/**
* Commits a transaction
*
113,7 → 151,7
{
return $this->_database->commit();
}
 
/**
* Retrieves all rows from the table
*
124,7 → 162,7
{
return $this->_database->fetchAll($this->_name, $fetch_style, $column_index, $ctor_args);
}
 
/**
* Selects data from one or more tables
*
135,7 → 173,7
{
return $this->_database->select($this->_name, $columns, $where, $order, $limit);
}
 
/**
* Updates records in one or more tables
*
146,7 → 184,7
{
return $this->_database->update($this->_name, $data, $condition);
}
 
/**
* Inserts a record into the table
*
157,7 → 195,7
{
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.
169,7 → 207,7
{
return $this->_database->lastInsertId;
}
 
/**
* Delete a record from the table
*
185,21 → 223,21
* <var>$condition</var> are <code>null</code>.
* @see Database::delete()
*/
public function delete($id, array $condition = null)
public function delete ($id, array $condition = null)
{
if (!is_null($id))
if ($id !== null)
{
$condition = array($this->_id => $id);
}
else if (is_null($condition))
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
*
219,7 → 257,7
return $this->update($data, $condition);
}
 
return $this->insert($data);
return $this->insert($data);
}
 
/**
228,7 → 266,7
* @param mixed $id
* @return array
*/
public function find($id)
public function find ($id)
{
/* DEBUG */
if (defined('DEBUG') && DEBUG > 0)
235,14 → 273,14
{
debug($id);
}
 
$result = $this->select(null, array($this->_id => $id));
 
if ($result)
{
$result = $result[0];
}
 
return $result;
}
}