Subversion Repositories PHPX

Compare Revisions

Last modification

Ignore whitespace Rev 50 → Rev 49

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