Skip to content

Commit

Permalink
require only $_columns and $_primaryKeys, instead of all the differen…
Browse files Browse the repository at this point in the history
…t variants
  • Loading branch information
danomatic committed Mar 31, 2017
1 parent 93f57a3 commit abcabe6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 71 deletions.
114 changes: 64 additions & 50 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,17 @@ abstract class Model implements JsonSerializable {
*/
protected static $_primaryKeys;

/**
* string name of the primary key column
* @var string
*/
protected static $_primaryKey;

/**
* true if primary key is an auto-increment column
* @var bool
*/
protected static $_isAutoIncrement = false;

/**
* array of all fully-qualified(table.column) columns
* @var string[]
*/
protected static $_columns;

/**
* array of all column names
* @var string[]
*/
protected static $_columnNames;

/**
* array of all column types
* @var string[]
*/
protected static $_columnTypes;
protected static $_columns;

/**
* Array to contain names of modified columns
Expand Down Expand Up @@ -196,6 +178,24 @@ abstract class Model implements JsonSerializable {
*/
protected $_validationErrors = array();

/**
* Cache of lower case column names. Just for performance.
* @var string[]
*/
private static $_lowerCaseColumns = array();

/**
* Cache of column names. Just for performance.
* @var string[]
*/
private static $_columnNames = array();

/**
* Cache of fully qualified column names. Just for performance.
* @var string[]
*/
private static $_qualifiedColumns = array();

public function __toString() {
return get_class($this) . implode('-', $this->getPrimaryKeyValues());
}
Expand Down Expand Up @@ -289,42 +289,52 @@ static function getTableName() {
* @return array
*/
static function getColumnNames() {
return static::$_columnNames;
$class = get_called_class();
if (!isset(self::$_columnNames[$class])) {
self::$_columnNames[$class] = array_keys(static::$_columns);
}

return self::$_columnNames[$class];
}

/**
* Access to array of fully-qualified(table.column) columns
* @return array
*/
static function getColumns() {
return static::$_columns;
$class = get_called_class();
if (!isset(self::$_qualifiedColumns[$class])) {
self::$_qualifiedColumns[$class] = array_map(function($column_name) {
return static::getTableName() . '.' . $column_name;
}, array_keys(static::$_columns));
}

return self::$_qualifiedColumns[$class];
}

/**
* Access to array of column types, indexed by column name
* @return array
*/
static function getColumnTypes() {
return static::$_columnTypes;
return static::$_columns;
}

/**
* Get the type of a column
* @return string
*/
static function getColumnType($column_name) {
return static::$_columnTypes[static::normalizeColumnName($column_name)];
return static::$_columns[static::normalizeColumnName($column_name)];
}

private static $_lowerCaseColumns = array();

/**
* @return bool
*/
static function hasColumn($column_name) {
$class = get_called_class();
if (!isset(self::$_lowerCaseColumns[$class])) {
self::$_lowerCaseColumns[$class] = array_map('strtolower', static::$_columnNames);
self::$_lowerCaseColumns[$class] = array_map('strtolower', static::getColumnNames());
}
return in_array(
strtolower(static::normalizeColumnName($column_name)),
Expand All @@ -348,7 +358,10 @@ static function normalizeColumnName($column_name) {
* @return array
*/
static function getPrimaryKey() {
return static::$_primaryKey;
if (count(static::$_primaryKeys) === 1) {
return static::$_primaryKeys[0];
}
return null;
}

/**
Expand Down Expand Up @@ -419,7 +432,7 @@ static function coerceTemporalValue($value, $column_type, DABLPDO $conn = null)
* @return static
*/
static function retrieveByColumn($field, $value) {
$pk = static::$_primaryKey;
$pk = static::getPrimaryKey();
if ($pk) {
if ($field === $pk) {
return static::retrieveByPK($value);
Expand Down Expand Up @@ -758,7 +771,7 @@ static function fromResult(PDOStatement $result, $class_name = null, $use_pool =
* @param int $startcol
*/
function fromNumericResultArray($values, &$startcol) {
foreach (static::$_columnNames as &$column_name) {
foreach (static::$_columns as $column_name => &$type) {
$this->{$column_name} = $values[$startcol++];
}
if (static::$_primaryKeys && !$this->hasPrimaryKeyValues()) {
Expand All @@ -774,7 +787,7 @@ function fromNumericResultArray($values, &$startcol) {
* @param array $values
*/
function fromAssociativeResultArray($values) {
foreach (static::$_columnNames as &$column_name) {
foreach (static::$_columns as $column_name => &$type) {
if (array_key_exists($column_name, $values)) {
$this->{$column_name} = $values[$column_name];
}
Expand Down Expand Up @@ -840,12 +853,12 @@ static function insertBatch() {
return 0;
}
$conn = static::getConnection();
$columns = static::$_columnNames;
$columns = static::getColumnNames();
$quoted_table = $conn->quoteIdentifier(static::getTableName(), true);

$auto_increment = static::isAutoIncrement();
if ($auto_increment) {
$pk = static::$_primaryKey;
$pk = static::getPrimaryKey();
foreach ($columns as $index => &$column_name) {
if ($column_name === $pk) {
unset($columns[$index]);
Expand Down Expand Up @@ -1067,7 +1080,7 @@ function fromArray($array) {
foreach ($array as $column => &$v) {
if (
is_string($column) === false
|| !isset(static::$_columnTypes[$column])
|| !isset(static::$_columns[$column])
) {
continue;
}
Expand All @@ -1083,8 +1096,9 @@ function fromArray($array) {
*/
function toArray() {
$array = array();
foreach (static::$_columnNames as &$column)
$array[$column] = $this->{'get' . $column}();
foreach (static::$_columns as $column_name => &$type) {
$array[$column_name] = $this->{'get' . $column_name}();
}
return $array;
}

Expand All @@ -1096,11 +1110,11 @@ function toArray() {
*/
function jsonSerialize() {
$array = $this->toArray();
foreach (static::$_columnTypes as $column => &$type) {
if (!isset($array[$column])) {
foreach (static::$_columns as $column_name => &$type) {
if (!isset($array[$column_name])) {
continue;
}
$value = &$array[$column];
$value = &$array[$column_name];

if ($type === Model::COLUMN_TYPE_BOOLEAN) {
if (0 === $value) {
Expand All @@ -1113,14 +1127,14 @@ function jsonSerialize() {
|| $type === Model::COLUMN_TYPE_INTEGER_TIMESTAMP
) {
if (!$value) {
$array[$column] = null;
$array[$column_name] = null;
continue;
}
if (!is_int($value)) {
$value = strtotime($value);
}
if (!is_int($value)) {
throw new RuntimeException('Error parsing date "' . $array[$column] . '"');
throw new RuntimeException('Error parsing date "' . $array[$column_name] . '"');
}
$value = date('c', $value);
}
Expand Down Expand Up @@ -1320,15 +1334,15 @@ function setDirty($bool) {
* @return static
*/
function castInts() {
foreach (static::$_columnTypes as $column => &$type) {
if ($this->{$column} === null || !static::isIntegerType($type)) {
foreach (static::$_columns as $column_name => &$type) {
if ($this->{$column_name} === null || !static::isIntegerType($type)) {
continue;
}
if ('' === $this->{$column}) {
$this->{$column} = null;
if ('' === $this->{$column_name}) {
$this->{$column_name} = null;
continue;
}
$this->{$column} = (int) $this->{$column};
$this->{$column_name} = (int) $this->{$column_name};
}
return $this;
}
Expand All @@ -1339,16 +1353,16 @@ function castInts() {
*/
protected function insert() {
$conn = static::getConnection();
$pk = static::$_primaryKey;
$pk = static::getPrimaryKey();

$fields = array();
$values = array();
$placeholders = array();
foreach (static::$_columnNames as &$column) {
$value = $this->$column;
if ($value === null && !$this->isColumnModified($column))
foreach (static::$_columns as $column_name => &$type) {
$value = $this->$column_name;
if ($value === null && !$this->isColumnModified($column_name))
continue;
$fields[] = $conn->quoteIdentifier($column, true);
$fields[] = $conn->quoteIdentifier($column_name, true);
$values[] = $value;
$placeholders[] = '?';
}
Expand Down
38 changes: 17 additions & 21 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,7 @@ class TestModel extends Model {
'id',
);

protected static $_primaryKey = 'id';

public static $_columns = array(
'test_model.id',
'test_model.true_false',
'test_model.created',
'test_model.updated'
);

public static $_columnNames = array(
'id',
'true_false',
'created',
'updated'
);

public static $_columnTypes = array(
'id' => Model::COLUMN_TYPE_INTEGER,
'true_false' => Model::COLUMN_TYPE_BOOLEAN,
'created' => Model::COLUMN_TYPE_TIMESTAMP,
Expand Down Expand Up @@ -118,7 +102,7 @@ class TestModel2 extends Model {
'integerTimestamp',
);

public static $_columnTypes = array(
public static $_columns = array(
'nullInteger' => Model::COLUMN_TYPE_INTEGER,
'emptyInteger' => Model::COLUMN_TYPE_INTEGER,
'integer' => Model::COLUMN_TYPE_INTEGER,
Expand Down Expand Up @@ -346,21 +330,33 @@ public function testGetTableName() {
* @covers Model::getColumnNames
*/
public function testGetColumnNames() {
$this->assertEquals(TestModel::$_columnNames, TestModel::getColumnNames());
$column_names = array(
'id' ,
'true_false',
'created',
'updated'
);
$this->assertEquals($column_names, TestModel::getColumnNames());
}

/**
* @covers Model::getColumns
*/
public function testGetColumns() {
$this->assertEquals(TestModel::$_columns, TestModel::getColumns());
$column_names = array(
'test_model.id' ,
'test_model.true_false',
'test_model.created',
'test_model.updated'
);
$this->assertEquals($column_names, TestModel::getColumns());
}

/**
* @covers Model::getColumnTypes
* @covers Model::getColumns
*/
public function testGetColumnTypes() {
$this->assertEquals(TestModel::$_columnTypes, TestModel::getColumnTypes());
$this->assertEquals(TestModel::$_columns, TestModel::getColumnTypes());
}

/**
Expand Down

0 comments on commit abcabe6

Please sign in to comment.