17,16 → 17,21 |
{ |
/** |
* Name of the table |
* @var string |
*/ |
protected $_name = ''; |
protected static $_name = ''; |
|
/** |
* Database of the table |
* @var Database|string |
*/ |
protected $_database; |
protected static $_database; |
|
protected $_id = 'id'; |
/** |
* Name of the primary key column of the table |
* @var string |
*/ |
protected static $_id = 'id'; |
|
/** |
* Creates a new <code>Table</code> instance. |
47,7 → 52,7 |
* Name of the primary key column |
* @throws InvalidArgumentException |
*/ |
public function __construct($database = null, $name = '', $id = '') |
public function __construct ($database = null, $name = '', $id = '') |
{ |
if ($database === null) |
{ |
54,45 → 59,65 |
/* 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; |
$this->database = $database; |
} |
|
if ($name !== '') |
{ |
$this->_name = $name; |
$this->name = $name; |
} |
|
if (!\is_string($this->_name)) |
$name = $this->name; |
if (!\is_string($name)) |
{ |
throw new \InvalidArgumentException( |
'Expected string for table name, saw ' |
. \get_class($this->_name) || \gettype($this->_name)); |
. (\is_object($name) ? \get_class($name) : \gettype($name))); |
} |
|
if ($id !== '') |
{ |
$this->_id = $id; |
$this->id = $id; |
} |
} |
|
/** |
* @param string $value |
*/ |
public function setName ($value) |
{ |
$class = \get_class($this); |
$class::$_name = (string) $value; |
} |
|
/** |
* @return string |
*/ |
public function getName () |
{ |
$class = \get_class($this); |
return $class::$_name; |
} |
|
/** |
* Returns the database for the table |
* @return Database |
*/ |
public function getDatabase() |
{ |
if (\is_string($this->_database)) |
$class = \get_class($this); |
if (\is_string($class::$_database)) |
{ |
/* Call setter to convert to Database */ |
$this->database = $this->_database; |
$this->setDatabase($class::$_database); |
} |
|
return $this->_database; |
return $class::$_database; |
} |
|
/** |
101,9 → 126,10 |
*/ |
public function setDatabase ($value) |
{ |
$class = \get_class($this); |
if ($value instanceof Database) |
{ |
$this->_database = $value; |
$class::$_database = $value; |
} |
else if ($value !== null) |
{ |
112,15 → 138,33 |
{ |
throw new \InvalidArgumentException( |
'Expected Database instance or string for class name, saw ' |
. (\get_class($value) || \gettype($value)) |
. (\is_object($value) ? \get_class($value) : \gettype($value)) |
); |
} |
|
$this->_database = $database; |
$class::$_database = $database; |
} |
} |
|
/** |
* @param string $value |
*/ |
public function setId ($value) |
{ |
$class = \get_class($this); |
$class::$_id = (string) $value; |
} |
|
/** |
* @return string |
*/ |
public function getId () |
{ |
$class = \get_class($this); |
return $class::$_id; |
} |
|
/** |
* Initiates a transaction |
* |
* @return bool |
128,7 → 172,7 |
*/ |
public function beginTransaction() |
{ |
return $this->_database->beginTransaction(); |
return $this->database->beginTransaction(); |
} |
|
/** |
139,7 → 183,7 |
*/ |
public function rollBack() |
{ |
return $this->_database->rollBack(); |
return $this->database->rollBack(); |
} |
|
/** |
150,7 → 194,7 |
*/ |
public function commit() |
{ |
return $this->_database->commit(); |
return $this->database->commit(); |
} |
|
/** |
161,7 → 205,7 |
*/ |
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); |
return $this->database->fetchAll($this->name, $fetch_style, $column_index, $ctor_args); |
} |
|
/** |
172,7 → 216,7 |
*/ |
public function select($columns = null, $where = null, $order = null, $limit = null) |
{ |
return $this->_database->select($this->_name, $columns, $where, $order, $limit); |
return $this->database->select($this->name, $columns, $where, $order, $limit); |
} |
|
/** |
183,7 → 227,7 |
*/ |
public function update($data, $condition) |
{ |
return $this->_database->update($this->_name, $data, $condition); |
return $this->database->update($this->name, $data, $condition); |
} |
|
/** |
194,7 → 238,7 |
*/ |
public function insert($data, $cols = null) |
{ |
return $this->_database->insert($this->_name, $data, $cols); |
return $this->database->insert($this->name, $data, $cols); |
} |
|
/** |
206,7 → 250,7 |
*/ |
public function getLastInsertId() |
{ |
return $this->_database->lastInsertId; |
return $this->database->lastInsertId; |
} |
|
/** |
228,7 → 272,7 |
{ |
if ($id !== null) |
{ |
$condition = array($this->_id => $id); |
$condition = array($this->id => $id); |
} |
else if ($condition === null) |
{ |
236,7 → 280,7 |
'$id and $condition cannot both be null'); |
} |
|
return $this->_database->delete($this->_name, $condition); |
return $this->database->delete($this->name, $condition); |
} |
|
/** |
253,7 → 297,7 |
*/ |
public function updateOrInsert($data, array $condition = null) |
{ |
if ($this->select($this->_id, $condition)) |
if ($this->select($this->id, $condition)) |
{ |
return $this->update($data, $condition); |
} |
275,7 → 319,7 |
debug($id); |
} |
|
$result = $this->select(null, array($this->_id => $id)); |
$result = $this->select(null, array($this->id => $id)); |
|
if ($result) |
{ |