ArrayDatasource: Create a new result when counting

There is usually no limit and offset when a query is going to be counted
so the cached result must not be used.
This commit is contained in:
Johannes Meyer 2015-05-06 10:08:07 +02:00
parent f2ad2838f4
commit 7b2ed3bef7

View File

@ -22,6 +22,13 @@ class ArrayDatasource implements Selectable
*/ */
protected $result; protected $result;
/**
* The result of a counted query
*
* @var int
*/
protected $count;
/** /**
* The name of the column to map array keys on * The name of the column to map array keys on
* *
@ -89,6 +96,7 @@ class ArrayDatasource implements Selectable
$arr = (array) $row; $arr = (array) $row;
$result[] = array_shift($arr); $result[] = array_shift($arr);
} }
return $result; return $result;
} }
@ -110,8 +118,10 @@ class ArrayDatasource implements Selectable
$keys[1] = $keys[0]; $keys[1] = $keys[0];
} }
} }
$result[$row->{$keys[0]}] = $row->{$keys[1]}; $result[$row->{$keys[0]}] = $row->{$keys[1]};
} }
return $result; return $result;
} }
@ -152,27 +162,27 @@ class ArrayDatasource implements Selectable
*/ */
public function count(SimpleQuery $query) public function count(SimpleQuery $query)
{ {
$this->createResult($query); if ($this->count === null) {
return count($this->result); $this->count = count($this->createResult($query));
}
return $this->count;
} }
/** /**
* Create the result for the given query, in case there is none yet * Create and return the result for the given query
* *
* @param SimpleQuery $query * @param SimpleQuery $query
* *
* @return $this * @return array
*/ */
protected function createResult(SimpleQuery $query) protected function createResult(SimpleQuery $query)
{ {
if ($this->hasResult()) {
return $this;
}
$columns = $query->getColumns(); $columns = $query->getColumns();
$filter = $query->getFilter(); $filter = $query->getFilter();
$offset = $query->hasOffset() ? $query->getOffset() : 0; $offset = $query->hasOffset() ? $query->getOffset() : 0;
$limit = $query->hasLimit() ? $query->getLimit() : 0; $limit = $query->hasLimit() ? $query->getLimit() : 0;
$foundStringKey = false; $foundStringKey = false;
$result = array(); $result = array();
$skipped = 0; $skipped = 0;
@ -226,8 +236,7 @@ class ArrayDatasource implements Selectable
$result = array_values($result); $result = array_values($result);
} }
$this->setResult($result); return $result;
return $this;
} }
/** /**
@ -263,8 +272,9 @@ class ArrayDatasource implements Selectable
protected function getResult(SimpleQuery $query) protected function getResult(SimpleQuery $query)
{ {
if (! $this->hasResult()) { if (! $this->hasResult()) {
$this->createResult($query); $this->setResult($this->createResult($query));
} }
return $this->result; return $this->result;
} }
} }