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. |
21,7 → 20,7 |
|
/** |
* Database of the table |
* @var Database|string |
* @var Database |
*/ |
protected $_database; |
|
46,16 → 45,12 |
* 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(); |
} |
} |
else |
{ |
$this->_database = $database; |
66,11 → 61,9 |
$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 !== '') |
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 |
225,11 → 187,11 |
*/ |
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'); |