Commit 8df83e25 authored by Patrick Barroca's avatar Patrick Barroca 🐧

introduce fetchAllBy

parent e999f53a
Pipeline #9066 failed with stage
in 16 seconds
......@@ -456,4 +456,9 @@ class Storm_Model_Loader {
$this->cacheInstance($first_instance);
return $first_instance;
}
public function fetchAllBy($fields, $params) {
return $this->getPersistenceStrategy()->fetchAllBy($fields, $params);
}
}
......@@ -26,18 +26,20 @@ THE SOFTWARE.
class Storm_Model_PersistenceStrategy_Abstract {
protected $_loader;
protected $_loader;
public function __construct($loader){
$this->_loader=$loader;
}
public function isVolatile() {
public function isVolatile() {
return false;
}
}
?>
\ No newline at end of file
public function fetchAllBy($fields, $params) {
return [];
}
}
\ No newline at end of file
......@@ -33,6 +33,7 @@ class Storm_Model_PersistenceStrategy_Db extends Storm_Model_PersistenceStrategy
return $this->_loader->newFromRow($row);
}
public function getTable() {
if (!isset($this->_table)) {
$table_name = call_user_func_array([$this->_loader->getModel(),'getClassVar'],['_table_name']);
......@@ -40,7 +41,6 @@ class Storm_Model_PersistenceStrategy_Db extends Storm_Model_PersistenceStrategy
}
return $this->_table;
}
......@@ -73,26 +73,23 @@ class Storm_Model_PersistenceStrategy_Db extends Storm_Model_PersistenceStrategy
->delete($this->_loader->getIdField() . "='" . $model->getId() . "'");
}
public function findAll($select=null) {
if (is_string($select)) {
$rowset = $this->getTable()
->getAdapter()
->fetchAll($select);
} else {
$rowset = $this
->getTable()
->fetchAll($select);
public function findAll($select=null) {
if (is_string($select))
return $this->_findAllByString($select);
if ($rowset == null)
return [];
return ($rowset = $this->getTable()->fetchAll($select))
? array_map([$this->_loader, 'newFromRow'], $rowset->toArray())
: [];
}
$rowset = $rowset->toArray();
}
return array_map([$this->_loader, 'newFromRow'], $rowset);
protected function _findAllByString($query) {
return array_map([$this->_loader, 'newFromRow'],
$this->getTable()->getAdapter()->fetchAll($query));
}
public function findAllBy($args) {
if ($select = $this->_generateSelectFor($args))
return $this->findAll($select);
......@@ -100,6 +97,17 @@ class Storm_Model_PersistenceStrategy_Db extends Storm_Model_PersistenceStrategy
}
public function fetchAllBy($fields, $params) {
if (!$select = $this->_generateSelectFor($params))
return [];
$select->from($select->getTable(), $fields);
return ($rowset = $this->getTable()->fetchAll($select))
? $rowset->toArray()
: [];
}
public function countBy($args) {
if (!$select = $this->_generateSelectFor($args))
return 0;
......
......@@ -94,6 +94,42 @@ class Storm_Model_PersistenceStrategy_Volatile extends Storm_Model_PersistenceS
}
public function fetchAllBy($fields, $params) {
return array_map(function($model) use($fields)
{
$keys = array_map(function($field)
{
$parts = explode(' as ', $field);
return current($parts);
},
$fields);
$aliases = array_map(function($field)
{
$parts = explode(' as ', $field);
return isset($parts[1]) ? $parts[1] : current($parts);
},
$fields);
$map = array_combine($keys, $aliases);
$filtered = array_filter($model->getRawAttributes(),
function($key) use ($keys)
{
return in_array($key, $keys);
},
ARRAY_FILTER_USE_KEY);
$row = [];
foreach($filtered as $k => $v)
$row[$map[$k]] = $v;
return $row;
},
$this->findAll($params));
}
protected function extractRoleAndModel($select) {
if (array_key_exists('role', $select) && array_key_exists('model', $select)) {
$model = $select['model'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment