00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Data_Collection_Db extends Varien_Data_Collection
00036 {
00037
00038
00039
00040
00041
00042 protected $_conn;
00043
00044
00045
00046
00047
00048
00049 protected $_select;
00050
00051 protected $_cacheConf = null;
00052
00053
00054
00055
00056
00057
00058
00059
00060 protected $_idFieldName;
00061
00062 protected $_bindParams = array();
00063
00064
00065
00066
00067
00068
00069
00070 protected $_data = null;
00071
00072
00073
00074
00075
00076
00077 protected $_map = null;
00078
00079 public function __construct($conn=null)
00080 {
00081 parent::__construct();
00082
00083 if (!is_null($conn)) {
00084 $this->setConnection($conn);
00085 }
00086 }
00087
00088 public function addBindParam($name, $value)
00089 {
00090 $this->_bindParams[$name] = $value;
00091 return $this;
00092 }
00093
00094 public function initCache($object, $idPrefix, $tags)
00095 {
00096 $this->_cacheConf = array(
00097 'object' => $object,
00098 'prefix' => $idPrefix,
00099 'tags' => $tags
00100 );
00101 return $this;
00102 }
00103
00104 protected function _setIdFieldName($fieldName)
00105 {
00106 $this->_idFieldName = $fieldName;
00107 return $this;
00108 }
00109
00110 public function getIdFieldName()
00111 {
00112 return $this->_idFieldName;
00113 }
00114
00115 protected function _getItemId(Varien_Object $item)
00116 {
00117 if ($field = $this->getIdFieldName()) {
00118 return $item->getData($field);
00119 }
00120 return parent::_getItemId($item);
00121 }
00122
00123 public function setConnection($conn)
00124 {
00125 if (!$conn instanceof Zend_Db_Adapter_Abstract) {
00126 throw new Zend_Exception('dbModel read resource does not implement Zend_Db_Adapter_Abstract');
00127 }
00128
00129 $this->_conn = $conn;
00130 $this->_select = $this->_conn->select();
00131 }
00132
00133
00134
00135
00136
00137
00138 public function getSelect()
00139 {
00140 return $this->_select;
00141 }
00142
00143
00144
00145
00146
00147
00148 public function getConnection()
00149 {
00150 return $this->_conn;
00151 }
00152
00153
00154
00155
00156
00157
00158 public function getSize()
00159 {
00160 if (is_null($this->_totalRecords)) {
00161 $sql = $this->getSelectCountSql();
00162 $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
00163 }
00164 return intval($this->_totalRecords);
00165 }
00166
00167
00168
00169
00170
00171
00172 public function getSelectCountSql()
00173 {
00174 $this->_renderFilters();
00175
00176 $countSelect = clone $this->getSelect();
00177 $countSelect->reset(Zend_Db_Select::ORDER);
00178 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00179 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00180 $countSelect->reset(Zend_Db_Select::COLUMNS);
00181
00182 $countSelect->from('', 'COUNT(*)');
00183
00184 return $countSelect;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 function getSelectSql($stringMode = false)
00194 {
00195 if ($stringMode) {
00196 return $this->_select->__toString();
00197 }
00198 return $this->_select;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208 public function setOrder($field, $direction = self::SORT_ORDER_DESC)
00209 {
00210 return $this->_setOrder($field, $direction);
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220 public function addOrder($field, $direction = self::SORT_ORDER_DESC)
00221 {
00222 return $this->_setOrder($field, $direction);
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232 public function unshiftOrder($field, $direction = self::SORT_ORDER_DESC)
00233 {
00234 return $this->_setOrder($field, $direction, true);
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 private function _setOrder($field, $direction, $unshift = false)
00246 {
00247 $direction = (strtoupper($direction) == self::SORT_ORDER_ASC) ? self::SORT_ORDER_ASC : self::SORT_ORDER_DESC;
00248
00249 if ($unshift) {
00250 $orders = array($field => new Zend_Db_Expr($field . ' ' . $direction));
00251 foreach ($this->_orders as $key => $expression) {
00252 if (!isset($orders[$key])) {
00253 $orders[$key] = $expression;
00254 }
00255 }
00256 $this->_orders = $orders;
00257 }
00258 else {
00259 $this->_orders[$field] = new Zend_Db_Expr($field . ' ' . $direction);
00260 }
00261 return $this;
00262 }
00263
00264
00265
00266
00267
00268
00269 protected function _renderFilters()
00270 {
00271 if ($this->_isFiltersRendered) {
00272 return $this;
00273 }
00274
00275 foreach ($this->_filters as $filter) {
00276 switch ($filter['type']) {
00277 case 'or' :
00278 $condition = $this->_conn->quoteInto($filter['field'].'=?', $filter['value']);
00279 $this->_select->orWhere($condition);
00280 break;
00281 case 'string' :
00282 $this->_select->where($filter['value']);
00283 break;
00284 default:
00285 $condition = $this->_conn->quoteInto($filter['field'].'=?', $filter['value']);
00286 $this->_select->where($condition);
00287 }
00288 }
00289 $this->_isFiltersRendered = true;
00290 return $this;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 public function addFieldToFilter($field, $condition=null)
00308 {
00309 $field = $this->_getMappedField($field);
00310 $this->_select->where($this->_getConditionSql($field, $condition));
00311 return $this;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 protected function _getMappedField($field)
00321 {
00322 $mappedFiled = $field;
00323
00324 $mapper = $this->_getMapper();
00325
00326 if (isset($mapper['fields'][$field])) {
00327 $mappedFiled = $mapper['fields'][$field];
00328 }
00329
00330 return $mappedFiled;
00331 }
00332
00333 protected function _getMapper()
00334 {
00335 if (isset($this->_map)) {
00336 return $this->_map;
00337 }
00338 else {
00339 return false;
00340 }
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 protected function _getConditionSql($fieldName, $condition) {
00363 if (is_array($fieldName)) {
00364 foreach ($fieldName as $f) {
00365 $orSql = array();
00366 foreach ($condition as $orCondition) {
00367 $orSql[] = "(".$this->_getConditionSql($f[0], $f[1]).")";
00368 }
00369 $sql = "(".join(" or ", $orSql).")";
00370 }
00371 return $sql;
00372 }
00373
00374 $sql = '';
00375 $fieldName = $this->_getConditionFieldName($fieldName);
00376 if (is_array($condition)) {
00377 if (isset($condition['from']) || isset($condition['to'])) {
00378 if (isset($condition['from'])) {
00379 if (empty($condition['date'])) {
00380 if ( empty($condition['datetime'])) {
00381 $from = $condition['from'];
00382 }
00383 else {
00384 $from = $this->getConnection()->convertDateTime($condition['from']);
00385 }
00386 }
00387 else {
00388 $from = $this->getConnection()->convertDate($condition['from']);
00389 }
00390 $sql.= $this->getConnection()->quoteInto("$fieldName >= ?", $from);
00391 }
00392 if (isset($condition['to'])) {
00393 $sql.= empty($sql) ? '' : ' and ';
00394
00395 if (empty($condition['date'])) {
00396 if ( empty($condition['datetime'])) {
00397 $to = $condition['to'];
00398 }
00399 else {
00400 $to = $this->getConnection()->convertDateTime($condition['to']);
00401 }
00402 }
00403 else {
00404 $to = $this->getConnection()->convertDate($condition['to']);
00405 }
00406
00407 $sql.= $this->getConnection()->quoteInto("$fieldName <= ?", $to);
00408 }
00409 }
00410 elseif (isset($condition['eq'])) {
00411 $sql = $this->getConnection()->quoteInto("$fieldName = ?", $condition['eq']);
00412 }
00413 elseif (isset($condition['neq'])) {
00414 $sql = $this->getConnection()->quoteInto("$fieldName != ?", $condition['neq']);
00415 }
00416 elseif (isset($condition['like'])) {
00417 $sql = $this->getConnection()->quoteInto("$fieldName like ?", $condition['like']);
00418 }
00419 elseif (isset($condition['nlike'])) {
00420 $sql = $this->getConnection()->quoteInto("$fieldName not like ?", $condition['nlike']);
00421 }
00422 elseif (isset($condition['in'])) {
00423 $sql = $this->getConnection()->quoteInto("$fieldName in (?)", $condition['in']);
00424 }
00425 elseif (isset($condition['nin'])) {
00426 $sql = $this->getConnection()->quoteInto("$fieldName not in (?)", $condition['nin']);
00427 }
00428 elseif (isset($condition['is'])) {
00429 $sql = $this->getConnection()->quoteInto("$fieldName is ?", $condition['is']);
00430 }
00431 elseif (isset($condition['notnull'])) {
00432 $sql = "$fieldName is NOT NULL";
00433 }
00434 elseif (isset($condition['null'])) {
00435 $sql = "$fieldName is NULL";
00436 }
00437 elseif (isset($condition['moreq'])) {
00438 $sql = $this->getConnection()->quoteInto("$fieldName >= ?", $condition['moreq']);
00439 }
00440 elseif (isset($condition['gt'])) {
00441 $sql = $this->getConnection()->quoteInto("$fieldName > ?", $condition['gt']);
00442 }
00443 elseif (isset($condition['lt'])) {
00444 $sql = $this->getConnection()->quoteInto("$fieldName < ?", $condition['lt']);
00445 }
00446 elseif (isset($condition['gteq'])) {
00447 $sql = $this->getConnection()->quoteInto("$fieldName >= ?", $condition['gteq']);
00448 }
00449 elseif (isset($condition['lteq'])) {
00450 $sql = $this->getConnection()->quoteInto("$fieldName <= ?", $condition['lteq']);
00451 }
00452 elseif (isset($condition['finset'])) {
00453 $sql = $this->getConnection()->quoteInto("find_in_set(?,$fieldName)", $condition['finset']);
00454 }
00455 else {
00456 $orSql = array();
00457 foreach ($condition as $orCondition) {
00458 $orSql[] = "(".$this->_getConditionSql($fieldName, $orCondition).")";
00459 }
00460 $sql = "(".join(" or ", $orSql).")";
00461 }
00462 } else {
00463 $sql = $this->getConnection()->quoteInto("$fieldName = ?", (string)$condition);
00464 }
00465 return $sql;
00466 }
00467
00468 protected function _getConditionFieldName($fieldName)
00469 {
00470 return $fieldName;
00471 }
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601 protected function _renderOrders()
00602 {
00603 foreach ($this->_orders as $orderExpr) {
00604 $this->_select->order($orderExpr);
00605 }
00606 return $this;
00607 }
00608
00609
00610
00611
00612
00613
00614 protected function _renderLimit()
00615 {
00616 if($this->_pageSize){
00617 $this->_select->limitPage($this->getCurPage(), $this->_pageSize);
00618 }
00619
00620 return $this;
00621 }
00622
00623
00624
00625
00626
00627
00628 public function distinct($flag)
00629 {
00630 $this->_select->distinct($flag);
00631 return $this;
00632 }
00633
00634
00635
00636
00637
00638
00639 public function load($printQuery = false, $logQuery = false)
00640 {
00641 if ($this->isLoaded()) {
00642 return $this;
00643 }
00644
00645 $this->_renderFilters()
00646 ->_renderOrders()
00647 ->_renderLimit();
00648
00649 $this->printLogQuery($printQuery, $logQuery);
00650
00651 $data = $this->getData();
00652 $this->resetData();
00653
00654 if (is_array($data)) {
00655 foreach ($data as $row) {
00656 $item = $this->getNewEmptyItem();
00657 if ($this->getIdFieldName()) {
00658 $item->setIdFieldName($this->getIdFieldName());
00659 }
00660 $item->addData($row);
00661 $this->addItem($item);
00662 }
00663 }
00664
00665 $this->_setIsLoaded();
00666 $this->_afterLoad();
00667 return $this;
00668 }
00669
00670
00671
00672
00673
00674
00675 public function getData()
00676 {
00677 if ($this->_data === null) {
00678 $this->_renderFilters()
00679 ->_renderOrders()
00680 ->_renderLimit();
00681 $this->_data = $this->_fetchAll($this->_select);
00682 $this->_afterLoadData();
00683 }
00684 return $this->_data;
00685 }
00686
00687
00688
00689
00690
00691
00692 protected function _afterLoadData()
00693 {
00694 return $this;
00695 }
00696
00697
00698
00699
00700
00701
00702 public function resetData()
00703 {
00704 $this->_data = null;
00705 return $this;
00706 }
00707
00708 protected function _afterLoad()
00709 {
00710 return $this;
00711 }
00712
00713 public function loadData($printQuery = false, $logQuery = false)
00714 {
00715 return $this->load($printQuery, $logQuery);
00716 }
00717
00718
00719
00720
00721
00722
00723
00724
00725 public function printLogQuery($printQuery = false, $logQuery = false, $sql = null) {
00726 if ($printQuery) {
00727 echo is_null($sql) ? $this->getSelect()->__toString() : $sql;
00728 }
00729
00730 if ($logQuery){
00731 Mage::log(is_null($sql) ? $this->getSelect()->__toString() : $sql);
00732 }
00733 return $this;
00734 }
00735
00736
00737
00738
00739
00740
00741 protected function _reset()
00742 {
00743 $this->getSelect()->reset();
00744 $this->_initSelect();
00745 $this->_setIsLoaded(false);
00746 $this->_items = array();
00747 $this->_data = null;
00748 return $this;
00749 }
00750
00751
00752
00753
00754
00755
00756
00757 protected function _fetchAll($select)
00758 {
00759 if ($this->_canUseCache() && ($object = $this->_getCacheInstance())) {
00760 if ($data = $object->load($this->_getSelectCacheId($select))) {
00761 $data = unserialize($data);
00762 }
00763 else {
00764 $data = $this->getConnection()->fetchAll($select, $this->_bindParams);
00765 $object->save(serialize($data), $this->_getSelectCacheId($select), $this->_getCacheTags());
00766 }
00767 } else {
00768 $data = $this->getConnection()->fetchAll($select, $this->_bindParams);
00769 }
00770 return $data;
00771 }
00772
00773 protected function _canUseCache()
00774 {
00775 return false;
00776 }
00777
00778 protected function _getSelectCacheId($select)
00779 {
00780 $id = md5($select->__toString());
00781 if (isset($this->_cacheConf['prefix'])) {
00782 $id = $this->_cacheConf['prefix'].'_'.$id;
00783 }
00784 return $id;
00785 }
00786
00787
00788
00789
00790
00791
00792 protected function _getCacheInstance()
00793 {
00794 if (isset($this->_cacheConf['object'])) {
00795 return $this->_cacheConf['object'];
00796 }
00797 return false;
00798 }
00799
00800 protected function _getCacheTags()
00801 {
00802 if (isset($this->_cacheConf['tags'])) {
00803 return $this->_cacheConf['tags'];
00804 }
00805 return array();
00806 }
00807 }