Created
June 5, 2011 20:32
-
-
Save gergoerdosi/1009389 to your computer and use it in GitHub Desktop.
Patch to allow array value in KDatabaseTableAbstract->select()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: code/libraries/koowa/database/table/abstract.php | |
=================================================================== | |
--- code/libraries/koowa/database/table/abstract.php (revision 3441) | |
+++ code/libraries/koowa/database/table/abstract.php (working copy) | |
@@ -30,35 +30,35 @@ | |
* @var string | |
*/ | |
protected $_name; | |
- | |
+ | |
/** | |
* Base name of the table in the db schema | |
* | |
* @var string | |
*/ | |
protected $_base; | |
- | |
+ | |
/** | |
* Name of the identity column in the table | |
* | |
* @var string | |
*/ | |
protected $_identity_column; | |
- | |
+ | |
/** | |
* Array of column mappings by column name | |
* | |
* @var array | |
*/ | |
protected $_column_map = array(); | |
- | |
+ | |
/** | |
* Database adapter | |
* | |
* @var object | |
*/ | |
protected $_database = false; | |
- | |
+ | |
/** | |
* Row object or identifier (APP::com.COMPONENT.row.NAME) | |
* | |
@@ -79,9 +79,9 @@ | |
* @var array | |
*/ | |
protected $_defaults; | |
- | |
+ | |
/** | |
- * Object constructor | |
+ * Object constructor | |
* | |
* @param object An optional KConfig object with configuration options. | |
*/ | |
@@ -89,22 +89,22 @@ | |
{ | |
//If no config is passed create it | |
if(!isset($config)) $config = new KConfig(); | |
- | |
+ | |
parent::__construct($config); | |
- | |
+ | |
$this->_name = $config->name; | |
$this->_base = $config->base; | |
$this->_database = $config->database; | |
$this->_row = $config->row; | |
$this->_rowset = $config->rowset; | |
- | |
+ | |
//Check if the table exists | |
if(!$info = $this->getSchema()) { | |
throw new KDatabaseTableException('Table '.$this->_name.' does not exist'); | |
} | |
- | |
+ | |
// Set the identity column | |
- if(!isset($config->identity_column)) | |
+ if(!isset($config->identity_column)) | |
{ | |
foreach ($this->getColumns(true) as $column) | |
{ | |
@@ -115,28 +115,28 @@ | |
} | |
} | |
else $this->_identity_column = $config->identity_column; | |
- | |
+ | |
//Set the default column mappings | |
$this->_column_map = $config->column_map ? $config->column_map->toArray() : array(); | |
if(!isset( $this->_column_map['id']) && isset($this->_identity_column)) { | |
$this->_column_map['id'] = $this->_identity_column; | |
} | |
- | |
+ | |
// Set the column filters | |
- if(!empty($config->filters)) | |
+ if(!empty($config->filters)) | |
{ | |
foreach($config->filters as $column => $filter) { | |
$this->getColumn($column, true)->filter = KConfig::toData($filter); | |
- } | |
+ } | |
} | |
- | |
+ | |
// Mixin a command chain | |
$this->mixin(new KMixinCommandchain($config->append(array('mixer' => $this)))); | |
- | |
+ | |
// Set the table behaviors | |
if(!empty($config->behaviors)) { | |
$this->addBehavior($config->behaviors); | |
- } | |
+ } | |
} | |
/** | |
@@ -151,7 +151,7 @@ | |
{ | |
$package = $this->_identifier->package; | |
$name = $this->_identifier->name; | |
- | |
+ | |
$config->append(array( | |
'database' => KFactory::get('lib.koowa.database.adapter.mysqli'), | |
'row' => null, | |
@@ -167,21 +167,21 @@ | |
))->append( | |
array('base' => $config->name) | |
); | |
- | |
+ | |
parent::_initialize($config); | |
} | |
- | |
+ | |
/** | |
* Get the object identifier | |
- * | |
- * @return KIdentifier | |
+ * | |
+ * @return KIdentifier | |
* @see KObjectIdentifiable | |
*/ | |
public function getIdentifier() | |
{ | |
return $this->_identifier; | |
} | |
- | |
+ | |
/** | |
* Get the database adapter | |
* | |
@@ -203,7 +203,7 @@ | |
$this->_database = $database; | |
return $this; | |
} | |
- | |
+ | |
/** | |
* Test the connected status of the table | |
* | |
@@ -223,11 +223,11 @@ | |
{ | |
return $this->_name; | |
} | |
- | |
+ | |
/** | |
* Gets the base table name without the table prefix | |
- * | |
- * If the table type is 'VIEW' the base name will be the name of the base | |
+ * | |
+ * If the table type is 'VIEW' the base name will be the name of the base | |
* table that is connected to the view. If the table type is 'BASE' this | |
* function will return the same as {@link getName} | |
* | |
@@ -237,7 +237,7 @@ | |
{ | |
return $this->_base; | |
} | |
- | |
+ | |
/** | |
* Gets the primary key(s) of the table | |
* | |
@@ -247,7 +247,7 @@ | |
{ | |
$keys = array(); | |
$columns = $this->getColumns(true); | |
- | |
+ | |
foreach ($columns as $name => $description) | |
{ | |
if($description->primary) { | |
@@ -257,7 +257,7 @@ | |
return $keys; | |
} | |
- | |
+ | |
/** | |
* Register one or more behaviors to the table | |
* | |
@@ -267,23 +267,23 @@ | |
public function addBehavior($behaviors) | |
{ | |
$behaviors = (array) KConfig::toData($behaviors); | |
- | |
+ | |
foreach($behaviors as $behavior) | |
{ | |
- if (!($behavior instanceof KDatabaseBehaviorInterface)) { | |
- $behavior = $this->getBehavior($behavior); | |
- } | |
- | |
+ if (!($behavior instanceof KDatabaseBehaviorInterface)) { | |
+ $behavior = $this->getBehavior($behavior); | |
+ } | |
+ | |
$identifier = (string) $behavior->getIdentifier(); | |
- | |
+ | |
//Add the behavior | |
$this->getSchema()->behaviors[$identifier] = $behavior; | |
$this->getCommandChain()->enqueue($behavior); | |
} | |
- | |
+ | |
return $this; | |
} | |
- | |
+ | |
/** | |
* Get a behavior by identifier | |
* | |
@@ -302,19 +302,19 @@ | |
} | |
else $identifier = KFactory::identify($behavior); | |
} | |
- | |
+ | |
//Make sure we have an identfier string | |
$identifier = (string) $identifier; | |
- | |
+ | |
if(!isset($this->getSchema()->behaviors[$identifier])) { | |
$behavior = KDatabaseBehavior::factory($identifier, array_merge($config, array('mixer' => $this))); | |
} else { | |
$behavior = $this->getSchema()->behaviors[$identifier]; | |
} | |
- | |
+ | |
return $behavior; | |
} | |
- | |
+ | |
/** | |
* Gets the behaviors of the table | |
* | |
@@ -323,8 +323,8 @@ | |
public function getBehaviors() | |
{ | |
return $this->getSchema()->behaviors; | |
- } | |
- | |
+ } | |
+ | |
/** | |
* Gets the schema of the table | |
* | |
@@ -334,7 +334,7 @@ | |
public function getSchema() | |
{ | |
$result = null; | |
- | |
+ | |
if($this->isConnected()) | |
{ | |
try { | |
@@ -343,15 +343,15 @@ | |
throw new KDatabaseTableException($e->getMessage()); | |
} | |
} | |
- | |
+ | |
return $result; | |
} | |
- | |
+ | |
/** | |
* Get a column by name | |
* | |
* @param boolean If TRUE, get the column information from the base table. Default is FALSE. | |
- * @return KDatabaseColumn Returns a KDatabaseSchemaColumn object or NULL if the | |
+ * @return KDatabaseColumn Returns a KDatabaseSchemaColumn object or NULL if the | |
* column does not exist | |
*/ | |
public function getColumn($columnname, $base = false) | |
@@ -371,18 +371,18 @@ | |
{ | |
//Get the table name | |
$name = $base ? $this->getBase() : $this->getName(); | |
- | |
+ | |
//Get the columns from the schema | |
$columns = $this->getSchema($name)->columns; | |
- | |
+ | |
return $this->mapColumns($columns, true); | |
} | |
- | |
+ | |
/** | |
* Table map method | |
- * | |
- * This functions maps the column names to those in the table schema | |
* | |
+ * This functions maps the column names to those in the table schema | |
+ * | |
* @param array|string An associative array of data to be mapped, or a column name | |
* @param boolean If TRUE, perform a reverse mapping | |
* @return array|string The mapped data or column name | |
@@ -400,11 +400,11 @@ | |
if(isset($map[$column])) { | |
$column = $map[$column]; | |
} | |
- | |
+ | |
$result[$column] = $value; | |
} | |
- } | |
- | |
+ } | |
+ | |
if(is_string($data)) | |
{ | |
$result = $data; | |
@@ -412,10 +412,10 @@ | |
$result = $map[$data]; | |
} | |
} | |
- | |
+ | |
return $result; | |
} | |
- | |
+ | |
/** | |
* Gets the identitiy column of the table. | |
* | |
@@ -427,10 +427,10 @@ | |
if(isset($this->_identity_column)) { | |
$result = $this->_identity_column; | |
} | |
- | |
+ | |
return $result; | |
} | |
- | |
+ | |
/** | |
* Gets the unqiue columns of the table | |
* | |
@@ -440,20 +440,20 @@ | |
{ | |
$result = array(); | |
$columns = $this->getColumns(true); | |
- | |
+ | |
foreach($columns as $name => $description) | |
{ | |
if($description->unique) { | |
$result[$name] = $description; | |
} | |
} | |
- | |
+ | |
return $result; | |
} | |
- | |
+ | |
/** | |
* Get default values for all columns | |
- * | |
+ * | |
* @return array | |
*/ | |
public function getDefaults() | |
@@ -462,21 +462,21 @@ | |
{ | |
$defaults = array(); | |
$columns = $this->getColumns(); | |
- | |
+ | |
foreach($columns as $name => $description) { | |
$defaults[$name] = $description->default; | |
} | |
- | |
+ | |
$this->_defaults = $defaults; | |
} | |
- | |
+ | |
return $this->_defaults; | |
} | |
- | |
+ | |
/** | |
* Get a default by name | |
* | |
- * @return mixed Returns the column default value or NULL if the | |
+ * @return mixed Returns the column default value or NULL if the | |
* column does not exist | |
*/ | |
public function getDefault($columnname) | |
@@ -484,7 +484,7 @@ | |
$defaults = $this->getDefaults(); | |
return isset($defaults[$columnname]) ? $defaults[$columnname] : null; | |
} | |
- | |
+ | |
/** | |
* Get an instance of a row object for this table | |
* | |
@@ -497,19 +497,19 @@ | |
$identifier = clone $this->_identifier; | |
$identifier->path = array('database', 'row'); | |
$identifier->name = KInflector::singularize($this->_identifier->name); | |
- | |
+ | |
//The row default options | |
$options = array( | |
- 'table' => $this, | |
+ 'table' => $this, | |
'identity_column' => $this->mapColumns($this->getIdentityColumn(), true) | |
); | |
- | |
- $this->_row = KFactory::tmp($identifier, $options); | |
+ | |
+ $this->_row = KFactory::tmp($identifier, $options); | |
} | |
- | |
+ | |
return clone $this->_row; | |
} | |
- | |
+ | |
/** | |
* Get an instance of a rowset object for this table | |
* | |
@@ -521,16 +521,16 @@ | |
{ | |
$identifier = clone $this->_identifier; | |
$identifier->path = array('database', 'rowset'); | |
- | |
+ | |
//The row default options | |
$options = array( | |
- 'table' => $this, | |
+ 'table' => $this, | |
'identity_column' => $this->mapColumns($this->getIdentityColumn(), true) | |
); | |
- | |
- $this->_rowset = KFactory::tmp($identifier, $options); | |
+ | |
+ $this->_rowset = KFactory::tmp($identifier, $options); | |
} | |
- | |
+ | |
return clone $this->_rowset; | |
} | |
@@ -539,13 +539,13 @@ | |
* | |
* The name of the resulting row(set) class is based on the table class name | |
* eg Com<Mycomp>Table<Tablename> -> Com<Mycomp>Row(set)<Tablename> | |
- * | |
+ * | |
* This function will return an empty rowset if called without a parameter. | |
* | |
* @param mixed KDatabaseQuery, query string, array of row id's, or an id or null | |
* @param integer The database fetch style. Default FETCH_ROWSET. | |
- * @return KDatabaseRow or KDatabaseRowset depending on the mode. By default will | |
- * return a KDatabaseRowset | |
+ * @return KDatabaseRow or KDatabaseRowset depending on the mode. By default will | |
+ * return a KDatabaseRowset | |
*/ | |
public function select( $query = null, $mode = KDatabase::FETCH_ROWSET) | |
{ | |
@@ -558,17 +558,17 @@ | |
$query = $this->_database->getQuery() | |
->where($key, 'IN', $values); | |
} | |
- | |
+ | |
if(is_array($query) && !is_numeric(key($query))) | |
{ | |
$columns = $this->mapColumns($query); | |
- $query = $this->_database->getQuery(); | |
- | |
+ $query = $this->_database->getQuery(); | |
+ | |
foreach($columns as $column => $value) { | |
- $query->where($column, '=', $value); | |
+ $query->where($column, 'IN', $value); | |
} | |
} | |
- | |
+ | |
if($query instanceof KDatabaseQuery) | |
{ | |
if(!is_null($query->columns) && !count($query->columns)) { | |
@@ -579,37 +579,37 @@ | |
$query->from($this->getName().' AS tbl'); | |
} | |
} | |
- | |
+ | |
//Create commandchain context | |
$context = $this->getCommandContext(); | |
$context->operation = KDatabase::OPERATION_SELECT; | |
$context->query = $query; | |
$context->table = $this->getBase(); | |
$context->mode = $mode; | |
- | |
- if($this->getCommandChain()->run('before.select', $context) !== false) | |
- { | |
+ | |
+ if($this->getCommandChain()->run('before.select', $context) !== false) | |
+ { | |
//Fetch the data based on the fecthmode | |
if($context->query) | |
{ | |
$data = $this->_database->select($context->query, $context->mode, $this->getIdentityColumn()); | |
- | |
+ | |
//Map the columns | |
if (($context->mode != KDatabase::FETCH_FIELD) || ($context->mode != KDatabase::FETCH_FIELD_LIST)) | |
- { | |
+ { | |
if($context->mode % 2) | |
{ | |
foreach($data as $key => $value) { | |
$data[$key] = $this->mapColumns($value, true); | |
} | |
} | |
- else $data = $this->mapColumns(KConfig::toData($data), true); | |
+ else $data = $this->mapColumns(KConfig::toData($data), true); | |
} | |
} | |
- | |
+ | |
switch($context->mode) | |
{ | |
- case KDatabase::FETCH_ROW : | |
+ case KDatabase::FETCH_ROW : | |
{ | |
$context->data = $this->getRow(); | |
if(isset($data) && !empty($data)) { | |
@@ -617,8 +617,8 @@ | |
} | |
break; | |
} | |
- | |
- case KDatabase::FETCH_ROWSET : | |
+ | |
+ case KDatabase::FETCH_ROWSET : | |
{ | |
$context->data = $this->getRowset(); | |
if(isset($data) && !empty($data)) { | |
@@ -626,16 +626,16 @@ | |
} | |
break; | |
} | |
- | |
+ | |
default : $context->data = $data; | |
} | |
- | |
+ | |
$this->getCommandChain()->run('after.select', $context); | |
} | |
- | |
+ | |
return KConfig::toData($context->data); | |
} | |
- | |
+ | |
/** | |
* Count table rows | |
* | |
@@ -648,13 +648,13 @@ | |
if(is_array($query) && !is_numeric(key($query))) | |
{ | |
$columns = $this->mapColumns($query); | |
- | |
- $query = $this->_database->getQuery(); | |
+ | |
+ $query = $this->_database->getQuery(); | |
foreach($columns as $column => $value) { | |
$query->where($column, '=', $value); | |
- } | |
+ } | |
} | |
- | |
+ | |
if($query instanceof KDatabaseQuery) | |
{ | |
$query->count(); | |
@@ -663,8 +663,8 @@ | |
$query->from($this->getName().' AS tbl'); | |
} | |
} | |
- | |
- $result = (int) $this->select($query, KDatabase::FETCH_FIELD); | |
+ | |
+ $result = (int) $this->select($query, KDatabase::FETCH_FIELD); | |
return $result; | |
} | |
@@ -683,33 +683,33 @@ | |
$context->table = $this->getBase(); | |
$context->query = null; | |
$context->affected = false; | |
- | |
- if($this->getCommandChain()->run('before.insert', $context) !== false) | |
+ | |
+ if($this->getCommandChain()->run('before.insert', $context) !== false) | |
{ | |
//Filter the data and remove unwanted columns | |
$data = $this->filter($context->data->getData(), true); | |
- | |
+ | |
//Get the data and apply the column mappings | |
$data = $this->mapColumns($data); | |
- | |
+ | |
//Execute the insert query | |
$context->affected = $this->_database->insert($context->table, $data); | |
- | |
- if($context->affected !== false) | |
+ | |
+ if($context->affected !== false) | |
{ | |
if(((integer) $context->affected) > 0) | |
{ | |
if($this->getIdentityColumn()) { | |
$data[$this->getIdentityColumn()] = $this->_database->getInsertId(); | |
} | |
- | |
+ | |
//Reverse apply the column mappings and set the data in the row | |
$context->data->setData($this->mapColumns($data, true), false) | |
->setStatus(KDatabase::STATUS_INSERTED); | |
} | |
else $context->data->setStatus(KDatabase::STATUS_FAILED); | |
} | |
- | |
+ | |
$this->getCommandChain()->run('after.insert', $context); | |
} | |
@@ -731,27 +731,27 @@ | |
$context->table = $this->getBase(); | |
$context->query = null; | |
$context->affected = false; | |
- | |
- if($this->getCommandChain()->run('before.update', $context) !== false) | |
+ | |
+ if($this->getCommandChain()->run('before.update', $context) !== false) | |
{ | |
//Create where statement | |
$query = $this->_database->getQuery(); | |
- | |
+ | |
//@TODO : Gracefully handle error if not all primary keys are set in the row | |
foreach($this->getPrimaryKey() as $key => $column) { | |
$query->where($column->name, '=', $this->filter(array($key => $context->data->$key), true)); | |
} | |
- | |
+ | |
//Filter the data and remove unwanted columns | |
$data = $this->filter($context->data->getData(true), true); | |
- | |
+ | |
//Get the data and apply the column mappings | |
$data = $this->mapColumns($data); | |
- | |
+ | |
//Execute the update query | |
$context->affected = $this->_database->update($context->table, $data, $query); | |
- | |
- if($context->affected !== false) | |
+ | |
+ if($context->affected !== false) | |
{ | |
if(((integer) $context->affected) > 0) | |
{ | |
@@ -760,11 +760,11 @@ | |
->setStatus(KDatabase::STATUS_UPDATED); | |
} | |
else $context->data->setStatus(KDatabase::STATUS_FAILED); | |
- } | |
- | |
+ } | |
+ | |
//Set the query in the context | |
$context->query = $query; | |
- | |
+ | |
$this->getCommandChain()->run('after.update', $context); | |
} | |
@@ -786,30 +786,30 @@ | |
$context->data = $row; | |
$context->query = null; | |
$context->affected = false; | |
- | |
- if($this->getCommandChain()->run('before.delete', $context) !== false) | |
+ | |
+ if($this->getCommandChain()->run('before.delete', $context) !== false) | |
{ | |
$query = $this->_database->getQuery(); | |
- | |
+ | |
//Create where statement | |
foreach($this->getPrimaryKey() as $key => $column) { | |
$query->where($column->name, '=', $context->data->$key); | |
} | |
- | |
+ | |
//Execute the delete query | |
$context->affected = $this->_database->delete($context->table, $query); | |
- | |
+ | |
//Set the query in the context | |
- if($context->affected !== false) | |
+ if($context->affected !== false) | |
{ | |
- if(((integer) $context->affected) > 0) | |
- { | |
+ if(((integer) $context->affected) > 0) | |
+ { | |
$context->query = $query; | |
$context->data->setStatus(KDatabase::STATUS_DELETED); | |
} | |
else $context->data->setStatus(KDatabase::STATUS_FAILED); | |
} | |
- | |
+ | |
$this->getCommandChain()->run('after.delete', $context); | |
} | |
@@ -829,15 +829,15 @@ | |
public function filter($data, $base = true) | |
{ | |
settype($data, 'array'); //force to array | |
- | |
+ | |
// Filter out any extra columns. | |
$data = array_intersect_key($data, $this->getColumns($base)); | |
- | |
+ | |
// Filter data based on column type | |
foreach($data as $key => $value) { | |
$data[$key] = $this->getColumn($key, $base)->filter->sanitize($value); | |
} | |
- | |
+ | |
return $data; | |
} | |
} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment