Skip to content

Instantly share code, notes, and snippets.

@gergoerdosi
Created June 5, 2011 20:29
Show Gist options
  • Save gergoerdosi/1009385 to your computer and use it in GitHub Desktop.
Save gergoerdosi/1009385 to your computer and use it in GitHub Desktop.
Patch to allow array value in KDatabaseRowsetAbstract->find()
Index: code/libraries/koowa/database/rowset/abstract.php
===================================================================
--- code/libraries/koowa/database/rowset/abstract.php (revision 3441)
+++ code/libraries/koowa/database/rowset/abstract.php (working copy)
@@ -26,14 +26,14 @@
* @var string
*/
protected $_identity_column;
-
+
/**
* Row object or identifier (APP::com.COMPONENT.row.NAME)
*
* @var string|object
*/
protected $_row;
-
+
/**
* Constructor
*
@@ -43,22 +43,22 @@
{
//If no config is passed create it
if(!isset($config)) $config = new KConfig();
-
+
parent::__construct($config);
-
+
$this->_row = $config->row;
-
+
// Set the table indentifier
if(isset($config->identity_column)) {
$this->_identity_column = $config->identity_column;
}
-
+
// Reset the rowset
$this->reset();
-
+
// Insert the data, if exists
if(!empty($config->data)) {
- $this->addData($config->data->toArray(), $config->new);
+ $this->addData($config->data->toArray(), $config->new);
}
}
@@ -76,23 +76,23 @@
'row' => null,
'data' => null,
'new' => true,
- 'identity_column' => null
+ 'identity_column' => null
));
parent::_initialize($config);
}
-
+
/**
* Get the object identifier
- *
- * @return KIdentifier
+ *
+ * @return KIdentifier
* @see KObjectIdentifiable
*/
public function getIdentifier()
{
return $this->_identifier;
}
-
+
/**
* Test the connected status of the rowset.
*
@@ -102,10 +102,10 @@
{
return true;
}
-
+
/**
* Add a row in the rowset
- *
+ *
* The row will be stored by it's identity_column if set or otherwise by
* it's object handle.
*
@@ -119,17 +119,17 @@
} else {
$handle = $row->getHandle();
}
-
+
if($handle) {
$this->_object_set->offsetSet($handle, $row);
}
-
+
return $this;
}
-
+
/**
* Removes a row
- *
+ *
* The row will be removed based on it's identity_column if set or otherwise by
* it's object handle.
*
@@ -143,14 +143,14 @@
} else {
$handle = $row->getHandle();
}
-
+
if($this->_object_set->offsetExists($handle)) {
$this->_object_set->offsetUnset($handle);
}
-
+
return $this;
}
-
+
/**
* Returns all data as an array.
*
@@ -165,7 +165,7 @@
}
return $result;
}
-
+
/**
* Set the rowset data based on a named array/hash
*
@@ -182,7 +182,7 @@
} else {
$data = (array) $data;
}
-
+
//Prevent changing the identity column
if(isset($this->_identity_column)) {
unset($data[$this->_identity_column]);
@@ -204,28 +204,28 @@
return $this;
}
-
+
/**
* Add rows to the rowset
*
- * @param array An associative array of row data to be inserted.
+ * @param array An associative array of row data to be inserted.
* @param boolean If TRUE, mark the row(s) as new (i.e. not in the database yet). Default TRUE
* @return void
* @see __construct
*/
public function addData(array $data, $new = true)
- {
+ {
//Set the data in the row object and insert the row
foreach($data as $k => $row)
{
$instance = $this->getRow()
->setData($row, $new)
->setStatus($new ? NULL : KDatabase::STATUS_LOADED);
-
+
$this->insert($instance);
}
}
-
+
/**
* Gets the identitiy column of the rowset
*
@@ -237,33 +237,33 @@
}
/**
- * Returns a KDatabaseRow
- *
+ * Returns a KDatabaseRow
+ *
* This functions accepts either a know position or associative array of key/value pairs
*
- * @param string|array The position or the key or an associatie array of column data
+ * @param string|array The position or the key or an associatie array of column data
* to match
* @return KDatabaseRow(set)Abstract Returns a row or rowset if successfull. Otherwise NULL.
*/
public function find($needle)
{
$result = null;
-
+
if(!is_scalar($needle))
{
$result = clone $this;
-
- foreach ($result as $i => $row)
- {
+
+ foreach ($result as $i => $row)
+ {
foreach($needle as $key => $value)
{
- if($row->{$key} != $value) {
+ if(!in_array($row->{$key}, (array) $value)) {
$result->extract($row);
- }
+ }
}
}
}
- else
+ else
{
if(isset($this->_object_set[$needle])) {
$result = $this->_object_set[$needle];
@@ -281,19 +281,19 @@
public function save()
{
$result = false;
-
+
if(count($this))
{
$result = true;
-
- foreach ($this as $i => $row)
+
+ foreach ($this as $i => $row)
{
if(!$row->save()) {
$result = false;
}
}
- }
-
+ }
+
return $result;
}
@@ -305,19 +305,19 @@
public function delete()
{
$result = false;
-
+
if(count($this))
{
$result = true;
-
- foreach ($this as $i => $row)
+
+ foreach ($this as $i => $row)
{
if(!$row->delete()) {
$result = false;
}
}
- }
-
+ }
+
return $result;
}
@@ -332,7 +332,7 @@
return true;
}
-
+
/**
* Get an instance of a row object for this rowset
*
@@ -345,18 +345,18 @@
$identifier = clone $this->_identifier;
$identifier->path = array('database', 'row');
$identifier->name = KInflector::singularize($this->_identifier->name);
-
+
//The row default options
$options = array(
'identity_column' => $this->getIdentityColumn()
);
-
- $this->_row = KFactory::tmp($identifier, $options);
+
+ $this->_row = KFactory::tmp($identifier, $options);
}
-
+
return clone $this->_row;
}
-
+
/**
* Retrieve an array of column values
*
@@ -367,7 +367,7 @@
{
$result = array();
foreach($this as $key => $row) {
- $result[$key] = $row->$column;
+ $result[$key] = $row->$column;
}
return $result;
@@ -387,7 +387,7 @@
$row->$column = $value;
}
}
-
+
/**
* Return an associative array of the data.
*
@@ -401,13 +401,13 @@
}
return $result;
}
-
+
/**
* Search the mixin method map and call the method or forward the call to
* each row for processing.
- *
+ *
* Function is also capable of checking is a behavior has been mixed succesfully
- * using is[Behavior] function. If the behavior exists the function will return
+ * using is[Behavior] function. If the behavior exists the function will return
* TRUE, otherwise FALSE.
*
* @param string The function name
@@ -436,7 +436,7 @@
foreach ($this as $i => $row) {
$row->__call($method, $arguments);
}
-
+
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment