summaryrefslogtreecommitdiffstats
path: root/src/Psecio/Gatekeeper/DataSource/Mysql.php
diff options
context:
space:
mode:
authorChris Cornutt <enygma@phpdeveloper.org>2015-01-19 05:38:16 -0600
committerChris Cornutt <enygma@phpdeveloper.org>2015-01-19 05:38:16 -0600
commitf603aa73081383a5806802bff92db6fe7b7f4970 (patch)
treee8dad6d892a61522e51c4c8e3f8a21a69afa2ef4 /src/Psecio/Gatekeeper/DataSource/Mysql.php
parent40a14a316f3b9151b832e756645f5325c77eb007 (diff)
downloadgatekeeper-f603aa73081383a5806802bff92db6fe7b7f4970.zip
gatekeeper-f603aa73081383a5806802bff92db6fe7b7f4970.tar.gz
gatekeeper-f603aa73081383a5806802bff92db6fe7b7f4970.tar.bz2
updating findBy* and datasource find() to return collection instead of a single record if more than one result is found
Diffstat (limited to 'src/Psecio/Gatekeeper/DataSource/Mysql.php')
-rw-r--r--src/Psecio/Gatekeeper/DataSource/Mysql.php21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/Psecio/Gatekeeper/DataSource/Mysql.php b/src/Psecio/Gatekeeper/DataSource/Mysql.php
index 77de1d8..645055f 100644
--- a/src/Psecio/Gatekeeper/DataSource/Mysql.php
+++ b/src/Psecio/Gatekeeper/DataSource/Mysql.php
@@ -200,13 +200,30 @@ class Mysql extends \Psecio\Gatekeeper\DataSource
$update[] = $column.' = '.$name;
}
- $sql = 'select * from '.$model->getTableName().' where '.implode(' and ', $update);
+ $sql = 'select * from '.$model->getTableName();
+ if (!empty($update)) {
+ $sql .= ' where '.implode(' and ', $update);
+ }
+
$result = $this->fetch($sql, $where);
if ($result !== false && count($result) == 1) {
$model->load($result[0]);
+ return $model;
+ } else {
+ // Make a collection instead
+ $modelClass = get_class($model);
+ $collectionNs = str_replace('Model', 'Collection', $modelClass);
+ if (!class_exists($collectionNs)) {
+ throw new \InvalidArgumentException('Collection "'.$collectionNs.'" is invalid!');
+ }
+ $collection = new $collectionNs($this);
+ foreach ($result as $item) {
+ $itemModel = new $modelClass($this, $item);
+ $collection->add($itemModel);
+ }
+ return $collection;
}
- return $model;
}
/**