/frameworks/solar/1.1.1/source/solar/Solar/Sql/Model/Params/Eager.php

https://github.com/ggunlugu/ornekler · PHP · 250 lines · 119 code · 12 blank · 119 comment · 21 complexity · 1c03267a98e5573722904f8036722d2e MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * A value-object to represent the various parameters available when eager-
  5. * fetching related records in a model fetch() call.
  6. *
  7. * @category Solar
  8. *
  9. * @package Solar_Sql_Model
  10. *
  11. * @author Paul M. Jones <pmjones@solarphp.com>
  12. *
  13. * @license http://opensource.org/licenses/bsd-license.php BSD
  14. *
  15. * @version $Id: Eager.php 4416 2010-02-23 19:52:43Z pmjones $
  16. *
  17. */
  18. class Solar_Sql_Model_Params_Eager extends Solar_Sql_Model_Params {
  19. /**
  20. *
  21. * Default data array.
  22. *
  23. * @var array
  24. *
  25. */
  26. protected $_data = array(
  27. 'eager' => array(),
  28. 'alias' => null,
  29. 'cols' => array(),
  30. 'merge' => null,
  31. 'native_by' => null,
  32. 'wherein_max' => null,
  33. 'cols_prefix' => null,
  34. 'join_type' => null,
  35. 'conditions' => array(),
  36. 'join_flag' => null,
  37. 'join_only' => null,
  38. );
  39. /**
  40. *
  41. * Sets the merge type to use ('client' or 'server').
  42. *
  43. * @param string $val The merge type to use ('client' or 'server').
  44. *
  45. * @return Solar_Sql_Model_Params_Eager
  46. *
  47. */
  48. public function merge($val)
  49. {
  50. $val = strtolower($val);
  51. if (! $val) {
  52. $this->_data['merge'] = null;
  53. } elseif ($val == 'client' || $val == 'server') {
  54. $this->_data['merge'] = $val;
  55. } else {
  56. throw $this->_exception('ERR_UNKNOWN_MERGE', array(
  57. 'merge' => $val,
  58. 'known' => '"client" or "server"',
  59. ));
  60. }
  61. return $this;
  62. }
  63. /**
  64. *
  65. * Sets the column-prefix to use when selecting columns.
  66. *
  67. * @param string $val The column-prefix to use.
  68. *
  69. * @return Solar_Sql_Model_Params_Eager
  70. *
  71. */
  72. public function colsPrefix($val)
  73. {
  74. $val = (string) $val;
  75. if (! $val) {
  76. $this->_data['cols_prefix'] = null;
  77. } else {
  78. $this->_data['cols_prefix'] = (string) $val;
  79. }
  80. return $this;
  81. }
  82. /**
  83. *
  84. * Sets the join type to use (null, 'left', or 'inner').
  85. *
  86. * @param string $val The join type to use (null, 'left', or 'inner').
  87. *
  88. * @return Solar_Sql_Model_Params_Eager
  89. *
  90. */
  91. public function joinType($val)
  92. {
  93. $val = strtolower($val);
  94. if (! $val) {
  95. $this->_data['join_type'] = null;
  96. } elseif ($val == 'left' || $val == 'inner') {
  97. $this->_data['join_type'] = $val;
  98. } else {
  99. throw $this->_exception('ERR_UNKNOWN_JOIN_TYPE', array(
  100. 'join_type' => $val,
  101. 'known' => 'null, "left", or "inner"',
  102. ));
  103. }
  104. return $this;
  105. }
  106. /**
  107. *
  108. * Sets the join condition to use; note that this overrides the existing
  109. * join condition.
  110. *
  111. * @param string $cond The ON condition.
  112. *
  113. * @param string $val A value to quote into the condition, replacing
  114. * question-mark placeholders.
  115. *
  116. * @return Solar_Sql_Model_Params_Eager
  117. *
  118. */
  119. public function addCondition($cond, $val = Solar_Sql_Select::IGNORE)
  120. {
  121. // BC-helping logic
  122. if (is_int($cond) && is_string($val)) {
  123. $cond = $val;
  124. $val = Solar_Sql_Select::IGNORE;
  125. }
  126. // now the real logic. use triple-equals so that empties are honored.
  127. if ($val === Solar_Sql_Select::IGNORE) {
  128. $this->_data['conditions'][] = $cond;
  129. } else {
  130. $this->_data['conditions'][$cond] = $val;
  131. }
  132. return $this;
  133. }
  134. /**
  135. *
  136. * Sets the join flag; i.e., whether or not this eager should be used to
  137. * control which parent records are selected.
  138. *
  139. * @param bool $val The join flag setting.
  140. *
  141. * @return Solar_Sql_Model_Params_Eager
  142. *
  143. */
  144. public function joinFlag($val)
  145. {
  146. $val = ($val === null) ? null : (bool) $val;
  147. $this->_data['join_flag'] = $val;
  148. return $this;
  149. }
  150. /**
  151. *
  152. * Whether or not this is a "join-only"; in a join-only, the eager is
  153. * joined, but no rows are selected.
  154. *
  155. * @param bool $val The join-only setting.
  156. *
  157. * @return Solar_Sql_Model_Params_Eager
  158. *
  159. */
  160. public function joinOnly($val)
  161. {
  162. $val = ($val === null) ? null : (bool) $val;
  163. $this->_data['join_only'] = $val;
  164. return $this;
  165. }
  166. /**
  167. *
  168. * Should native records be selected by "WHERE IN (...)" a list of IDs,
  169. * or by a sub-SELECT?
  170. *
  171. * @param bool $val The setting of 'wherein' or 'select'.
  172. *
  173. * @return Solar_Sql_Model_Params_Eager
  174. *
  175. */
  176. public function nativeBy($val)
  177. {
  178. $val = strtolower($val);
  179. if (! $val) {
  180. $this->_data['native_by'] = null;
  181. } elseif ($val == 'wherein' || $val == 'select') {
  182. $this->_data['native_by'] = $val;
  183. } else {
  184. throw $this->_exception('ERR_UNKNOWN_NATIVE_BY', array(
  185. 'native_by' => $val,
  186. 'known' => '"wherein" or "select"',
  187. ));
  188. }
  189. return $this;
  190. }
  191. /**
  192. *
  193. * When automatically choosing a "native-by" strategy, the maximum number
  194. * of records to use a "WHERE IN (...)" for; past this amount, use a sub-
  195. * SELECT.
  196. *
  197. * @param bool $val The setting of 'wherein' or 'select'.
  198. *
  199. * @return Solar_Sql_Model_Params_Eager
  200. *
  201. */
  202. public function whereinMax($val)
  203. {
  204. $val = ($val === null) ? null : (int) $val;
  205. $this->_data['wherein_max'] = $val;
  206. return $this;
  207. }
  208. /**
  209. *
  210. * Loads this params object with an array of data using support methods.
  211. *
  212. * @param array $data The data to load.
  213. *
  214. * @return Solar_Sql_Model_Params_Eager
  215. *
  216. * @see _loadOne()
  217. *
  218. * @see _loadTwo()
  219. *
  220. */
  221. protected function _load($data)
  222. {
  223. parent::_load($data);
  224. $this->_loadOne($data, array(
  225. 'merge',
  226. 'order',
  227. 'cols_prefix' => 'colsPrefix',
  228. 'join_type' => 'joinType',
  229. 'join_flag' => 'joinFlag',
  230. 'join_only' => 'joinOnly',
  231. 'native_by' => 'nativeBy',
  232. 'wherein_max' => 'whereinMax',
  233. ));
  234. $this->_loadTwo($data, array(
  235. 'conditions' => 'addCondition',
  236. ));
  237. }
  238. }