You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php namespace Majestic\Model;
  2. class SqlResultProvider implements iSqlResultItems
  3. {
  4. /**
  5. * @var DbStatement
  6. */
  7. private $result;
  8. /**
  9. * @var array
  10. * @desc my be changed in assoc method
  11. */
  12. private $result_items;
  13. /**
  14. * @var array
  15. */
  16. private $result_items_base;
  17. /**
  18. * @param $result DbStatement|array
  19. */
  20. public function __construct($result)
  21. {
  22. $this->result = $result;
  23. }
  24. private function defineResultItems()
  25. {
  26. if (is_null($this->result_items_base)) {
  27. if (is_array($this->result)) {
  28. $this->result_items_base = $this->result;
  29. } else {
  30. $this->result_items_base = $this->result->fetchAll();
  31. }
  32. $this->result_items = $this->result_items_base;
  33. }
  34. }
  35. /**
  36. * @param $field string
  37. * @param bool $assoc_as_array
  38. * @return $this SqlResultProvider
  39. * @throws \ErrorException
  40. */
  41. public function assoc($field, $assoc_as_array = false)
  42. {
  43. $this->defineResultItems();
  44. $result_items_assoc = array();
  45. foreach ($this->result_items_base as $item) {
  46. if (!isset($item->{$field})) {
  47. throw new \ErrorException('Undefined field. ' . $field);
  48. }
  49. if ($assoc_as_array) {
  50. if (!isset($result_items_assoc[$item->{$field}])) {
  51. $result_items_assoc[$item->{$field}] = array();
  52. }
  53. $result_items_assoc[$item->{$field}][] = $item;
  54. } else {
  55. if (isset($result_items_assoc[$item->{$field}])) {
  56. throw new \ErrorException('Field not unique. May be use assoc_as_array. ' . $field);
  57. }
  58. $result_items_assoc[$item->{$field}] = $item;
  59. }
  60. }
  61. // Ассоциирование внутри каждого элемента массива
  62. if ($assoc_as_array) {
  63. foreach ($result_items_assoc as &$value) {
  64. $value = new SqlResultCollection($value);
  65. }
  66. }
  67. $this->result_items = $result_items_assoc;
  68. return $this;
  69. }
  70. public function getKeys()
  71. {
  72. $this->defineResultItems();
  73. return array_keys($this->result_items);
  74. }
  75. /**
  76. * @param $key
  77. * @return mixed
  78. * метод актуален после вызова assoc
  79. */
  80. public function fetchKey($key)
  81. {
  82. return $this->result_items[$key];
  83. }
  84. /**
  85. * @return DbStatement[]|SqlResultCollection[]
  86. */
  87. public function fetchAll()
  88. {
  89. $this->defineResultItems();
  90. return $this->result_items;
  91. }
  92. /**
  93. * @param $field
  94. * @return mixed
  95. */
  96. public function fetchField($field)
  97. {
  98. if ($this->result->numRows())
  99. {
  100. return $this->result->fetchField($field);
  101. } else {
  102. return false;
  103. }
  104. }
  105. /**
  106. * @return mixed
  107. */
  108. public function fetch()
  109. {
  110. return $this->result->fetch(Db::FETCH_OBJ);
  111. }
  112. /**
  113. * @return int
  114. */
  115. public function affectedRows()
  116. {
  117. return $this->result->affectedRows();
  118. }
  119. }