Add SqlCriteria.join(). Refactoring SqlResultCollection (add SqlResultCollection).

This commit is contained in:
Alexander Demidov
2014-03-06 15:26:59 +04:00
parent 740bd6b7f3
commit 3272d7a903
4 changed files with 113 additions and 19 deletions

View File

@ -5,23 +5,44 @@ class SqlResultProvider
/**
* @var array
*/
private $result;
/**
* @var array
* @desc my be changed in assoc method
*/
private $result_items;
/**
* @var array
*/
private $result_items_base;
/**
* @param $result_items array
* @param $result DbStatement
*/
public function __construct($result_items)
public function __construct($result)
{
$this->result_items = $result_items;
$this->result = $result;
}
public function assoc($field, $assoc_as_array = false)
private function defineResultItems()
{
if (is_null($this->result_items_base)) {
$this->result_items_base = $this->result_items;
$this->result_items_base = $this->result->fetchAll();
$this->result_items = $this->result_items_base;
}
}
/**
* @param $field string
* @param bool $assoc_as_array
* @return $this SqlResultProvider
* @throws ErrorException
*/
public function assoc($field, $assoc_as_array = false)
{
$this->defineResultItems();
$result_items_assoc = array();
foreach ($this->result_items_base as $item) {
if (!isset($item->{$field})) {
@ -42,7 +63,7 @@ class SqlResultProvider
// Ассоциирование внутри каждого элемента массива
if ($assoc_as_array) {
foreach ($result_items_assoc as &$value) {
$value = new SqlResultProvider($value);
$value = new SqlResultCollection($value);
}
}
$this->result_items = $result_items_assoc;
@ -51,18 +72,22 @@ class SqlResultProvider
public function getKeys()
{
return array_keys($this->result_items);
return array_keys($this->result);
}
/**
* @return DbStatement[]|SqlResultCollection[]
*/
public function fetchAll()
{
$this->defineResultItems();
return $this->result_items;
}
/**
* @param $key
* @return mixed
* TODO: метод актуален только после вызова assoc
* метод актуален после вызова assoc
*/
public function fetchKey($key)
{
@ -72,19 +97,19 @@ class SqlResultProvider
/**
* @param $field
* @return mixed
* TODO: метод не актуален после вызова assoc с параметров assoc_as_array = true
*/
public function fetchField($field)
{
$item = current($this->result_items);
if ($item) {
return $item->{$field};
}
return false;
return $this->result->fetchField($field);
}
public function fetch()
public function fetch($style = Db::FETCH_OBJ)
{
return current($this->result_items);
return $this->result->fetch($style);
}
public function affectedRows()
{
return $this->result->affectedRows();
}
}