Dropped ID variable for Columns

The ID variable was used to map the table's UID so the model could inject it in properly. Added a new variable named columns that is an array of the key columns. Currently contains ID, Created at and Updated at columns. The timestamp columns will soon be injected into the queries and if the value is set to false, will skip it.
This commit is contained in:
Josh Sherman 2012-10-11 18:56:15 -04:00
parent fb4c8c1a5a
commit a595d85857
2 changed files with 50 additions and 38 deletions

View file

@ -34,6 +34,20 @@ class Model extends Object
*/
protected $db = null;
/**
* Columns
*
* Mapping of key columns for the table
*
* @access protected
* @var array
*/
protected $columns = array(
'id' => 'id',
'created_at' => 'created_at',
'updated_at' => 'updated_at',
);
/**
* Cache Object
*
@ -124,14 +138,6 @@ class Model extends Object
*/
protected $table = false; // FROM
/**
* ID Column
*
* @access protected
* @var string
*/
protected $id = 'id'; // WHERE ___ = ?
/**
* Joins
*
@ -352,12 +358,12 @@ class Model extends Object
}
elseif (ctype_digit((string)$type_or_parameters))
{
$this->loadParameters(array($this->id => $type_or_parameters));
$this->loadParameters(array($this->columns['id'] => $type_or_parameters));
$cache_key = 'PICKLES-' . $this->datasource . '-' . $this->table . '-' . $type_or_parameters;
}
elseif (ctype_digit((string)$parameters))
{
$this->loadParameters(array($this->id => $parameters));
$this->loadParameters(array($this->columns['id'] => $parameters));
}
// Starts with a basic SELECT ... FROM
@ -967,7 +973,7 @@ class Model extends Object
if (count($this->record) > 0)
{
// Determines if it's an UPDATE or INSERT
$update = (isset($this->record[$this->id]) && trim($this->record[$this->id]) != '');
$update = (isset($this->record[$this->columns['id']]) && trim($this->record[$this->columns['id']]) != '');
// Starts to build the query, optionally sets PRIORITY, DELAYED and IGNORE syntax
if ($this->replace === true && $this->mysql)
@ -1032,7 +1038,7 @@ class Model extends Object
// Loops through all the columns and assembles the query
foreach ($record as $column => $value)
{
if ($column != $this->id)
if ($column != $this->columns['id'])
{
if ($update === true)
{
@ -1055,12 +1061,12 @@ class Model extends Object
// If it's an UPDATE tack on the ID
if ($update === true)
{
$sql .= ' WHERE ' . $this->id . ' = :' . $this->id . ($this->mysql ? ' LIMIT 1' : '') . ';';
$input_parameters[':' . $this->id] = $this->record[$this->id];
$sql .= ' WHERE ' . $this->columns['id'] . ' = :' . $this->columns['id'] . ($this->mysql ? ' LIMIT 1' : '') . ';';
$input_parameters[':' . $this->columns['id']] = $this->record[$this->columns['id']];
if ($this->caching)
{
//$this->cache->delete('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $this->record[$this->id]);
//$this->cache->delete('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $this->record[$this->columns['id']]);
}
}
else
@ -1070,7 +1076,7 @@ class Model extends Object
// PDO::lastInsertId() doesn't work so we return the ID with the query
if ($this->postgresql)
{
$sql .= ' RETURNING ' . $this->id;
$sql .= ' RETURNING ' . $this->columns['id'];
}
$sql .= ';';
@ -1081,7 +1087,7 @@ class Model extends Object
{
$results = $this->db->fetch($sql, $input_parameters);
return $results[0][$this->id];
return $results[0][$this->columns['id']];
}
else
{
@ -1102,8 +1108,8 @@ class Model extends Object
*/
public function delete()
{
$sql = 'DELETE FROM ' . $this->table . ' WHERE ' . $this->id . ' = :' . $this->id . ($this->mysql ? ' LIMIT 1' : '') . ';';
$input_parameters[':' . $this->id] = $this->record[$this->id];
$sql = 'DELETE FROM ' . $this->table . ' WHERE ' . $this->columns['id'] . ' = :' . $this->columns['id'] . ($this->mysql ? ' LIMIT 1' : '') . ';';
$input_parameters[':' . $this->columns['id']] = $this->record[$this->columns['id']];
return $this->db->execute($sql, $input_parameters);
}