Public Member Functions | |
__construct ($conn=null) | |
addBindParam ($name, $value) | |
initCache ($object, $idPrefix, $tags) | |
getIdFieldName () | |
setConnection ($conn) | |
getSelect () | |
getConnection () | |
getSize () | |
getSelectCountSql () | |
getSelectSql ($stringMode=false) | |
setOrder ($field, $direction=self::SORT_ORDER_DESC) | |
addOrder ($field, $direction=self::SORT_ORDER_DESC) | |
unshiftOrder ($field, $direction=self::SORT_ORDER_DESC) | |
addFieldToFilter ($field, $condition=null) | |
distinct ($flag) | |
load ($printQuery=false, $logQuery=false) | |
getData () | |
resetData () | |
loadData ($printQuery=false, $logQuery=false) | |
printLogQuery ($printQuery=false, $logQuery=false, $sql=null) | |
Protected Member Functions | |
_setIdFieldName ($fieldName) | |
_getItemId (Varien_Object $item) | |
_renderFilters () | |
_getMappedField ($field) | |
_getMapper () | |
_getConditionSql ($fieldName, $condition) | |
_getConditionFieldName ($fieldName) | |
_renderOrders () | |
_renderLimit () | |
_afterLoadData () | |
_afterLoad () | |
_reset () | |
_fetchAll ($select) | |
_canUseCache () | |
_getSelectCacheId ($select) | |
_getCacheInstance () | |
_getCacheTags () | |
Protected Attributes | |
$_conn | |
$_select | |
$_cacheConf = null | |
$_idFieldName | |
$_bindParams = array() | |
$_data = null | |
$_map = null |
Definition at line 35 of file Db.php.
__construct | ( | $ | conn = null |
) |
Reimplemented in Mage_Core_Model_Mysql4_Collection_Abstract, and Mage_Eav_Model_Entity_Collection_Abstract.
Definition at line 79 of file Db.php.
00080 { 00081 parent::__construct(); 00082 00083 if (!is_null($conn)) { 00084 $this->setConnection($conn); 00085 } 00086 }
_afterLoad | ( | ) | [protected] |
Reimplemented in Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection, Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat_Collection, Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection, Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute_Collection, Mage_CatalogRule_Model_Mysql4_Rule_Collection, Mage_Cms_Model_Mysql4_Page_Collection, Mage_Eav_Model_Entity_Collection_Abstract, Mage_Review_Model_Mysql4_Review_Product_Collection, Mage_Rule_Model_Mysql4_Rule_Collection, Mage_Sales_Model_Entity_Quote_Item_Collection, Mage_Sales_Model_Mysql4_Order_Item_Collection, Mage_Sales_Model_Mysql4_Quote_Address_Item_Collection, Mage_Sales_Model_Mysql4_Quote_Item_Collection, and Mage_Tag_Model_Mysql4_Product_Collection.
Definition at line 708 of file Db.php.
_afterLoadData | ( | ) | [protected] |
Proces loaded collection data
Reimplemented in Mage_Eav_Model_Mysql4_Entity_Attribute_Collection.
Definition at line 692 of file Db.php.
_canUseCache | ( | ) | [protected] |
_fetchAll | ( | $ | select | ) | [protected] |
Fetch collection data
Zend_Db_Select | $select |
Definition at line 757 of file Db.php.
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 }
_getCacheInstance | ( | ) | [protected] |
_getCacheTags | ( | ) | [protected] |
Reimplemented in Mage_Core_Model_Mysql4_Collection_Abstract.
Definition at line 800 of file Db.php.
00801 { 00802 if (isset($this->_cacheConf['tags'])) { 00803 return $this->_cacheConf['tags']; 00804 } 00805 return array(); 00806 }
_getConditionFieldName | ( | $ | fieldName | ) | [protected] |
_getConditionSql | ( | $ | fieldName, | |
$ | condition | |||
) | [protected] |
Build SQL statement for condition
If $condition integer or string - exact value will be filtered
If $condition is array is - one of the following structures is expected:
If non matched - sequential array is expected and OR conditions will be built using above mentioned structure
string | $fieldName | |
integer|string|array | $condition |
Definition at line 362 of file Db.php.
00362 { 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 }
_getItemId | ( | Varien_Object $ | item | ) | [protected] |
Reimplemented from Varien_Data_Collection.
Definition at line 115 of file Db.php.
00116 { 00117 if ($field = $this->getIdFieldName()) { 00118 return $item->getData($field); 00119 } 00120 return parent::_getItemId($item); 00121 }
_getMappedField | ( | $ | field | ) | [protected] |
Try to get mapped field name for filter to collection
string |
Definition at line 320 of file Db.php.
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 }
_getMapper | ( | ) | [protected] |
_getSelectCacheId | ( | $ | select | ) | [protected] |
_renderFilters | ( | ) | [protected] |
Render sql select conditions
Reimplemented from Varien_Data_Collection.
Definition at line 269 of file Db.php.
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 }
_renderLimit | ( | ) | [protected] |
Render sql select limit
Reimplemented from Varien_Data_Collection.
Definition at line 614 of file Db.php.
00615 { 00616 if($this->_pageSize){ 00617 $this->_select->limitPage($this->getCurPage(), $this->_pageSize); 00618 } 00619 00620 return $this; 00621 }
_renderOrders | ( | ) | [protected] |
Build SQL statement for condition
If $condition integer or string - exact value will be filtered
If $condition is array is - one of the following structures is expected:
If non matched - sequential array is expected and OR conditions will be built using above mentioned structure
string | $fieldName | |
integer|string|array | $condition |
if (!empty($condition['datetime'])) { $argType = 'datetime'; } elseif (!empty($condition['date'])) { $argType = 'date'; } else { $argType = null; }
$sql = ''; foreach ($condition as $k=>$v) { $and = array(); $or = array();
if (is_numeric($k)) { $or[] = '('.$this->_getConditionSql($fieldName, $v).')'; }
switch ($k) { case 'null': if ($v==true) { $and[] = "$fieldName is null"; } elseif ($v==false) { $and[] = "$fieldName is not null"; } break;
case 'is': $and[] = $this->_read->quoteInto("$fieldName is ?", $v); break;
default: if (is_scalar($v)) { switch ($argType) { case 'date': $v = $this->_read->convertDate($v); break;
case 'datetime': $v = $this->_read->convertDateTime($v); break; } } }
switch ($k) { case '>=': case 'from': case 'gte': case 'gteq': $and[] = $this->_read->quoteInto("$fieldName >= ?", $v); break;
case '<=': case 'to': case 'lte': case 'lteq': $and[] = $this->_read->quoteInto("$fieldName <= ?", $v); break;
case '>': case 'gt': $and[] = $this->_read->quoteInto("$fieldName > ?", $v); break;
case '<': case 'lt': $and[] = $this->_read->quoteInto("$fieldName < ?", $v); break;
case '=': case '==': case 'eq': $and[] = $this->_read->quoteInto("$fieldName = ?", $v); break;
case '<>': case '!=': case 'neq': $and[] = $this->_read->quoteInto("$fieldName <> ?", $v); break;
case '': case 'like': $and[] = $this->_read->quoteInto("$fieldName like ?", $v); break;
case '!': case 'nlike': $and[] = $this->_read->quoteInto("$fieldName not like ?", $v); break;
case '()': case 'in': $and[] = $this->_read->quoteInto("$fieldName in (?)", $v); break;
case '!()': case 'nin': $and[] = $this->_read->quoteInto("$fieldName not in (?)", $v); break; } } if (!empty($and)) { $sql = join(" and ", $and); } if (!empty($or)) { if (!empty($sql)) { array_push($or, $sql); } $sql = '('.join(" or ", $or).')'; } return $sql; } Render sql select orders
Reimplemented from Varien_Data_Collection.
Definition at line 601 of file Db.php.
00602 { 00603 foreach ($this->_orders as $orderExpr) { 00604 $this->_select->order($orderExpr); 00605 } 00606 return $this; 00607 }
_reset | ( | ) | [protected] |
Reset collection
Reimplemented in Mage_Eav_Model_Entity_Collection_Abstract.
Definition at line 741 of file Db.php.
00742 { 00743 $this->getSelect()->reset(); 00744 $this->_initSelect(); 00745 $this->_setIsLoaded(false); 00746 $this->_items = array(); 00747 $this->_data = null; 00748 return $this; 00749 }
_setIdFieldName | ( | $ | fieldName | ) | [protected] |
addBindParam | ( | $ | name, | |
$ | value | |||
) |
addFieldToFilter | ( | $ | field, | |
$ | condition = null | |||
) |
Add field filter to collection
If $attribute is an array will add OR condition with following format: array( array('attribute'=>'firstname', 'like'=>'test'), array('attribute'=>'lastname', 'like'=>'test'), )
string|array | $attribute | |
null|string|array | $condition |
Reimplemented in Mage_Eav_Model_Entity_Collection_Abstract, Mage_GoogleBase_Model_Mysql4_Item_Collection, Mage_Log_Model_Mysql4_Visitor_Collection, Mage_Newsletter_Model_Mysql4_Queue_Collection, Mage_Newsletter_Model_Mysql4_Subscriber_Collection, Mage_Poll_Model_Mysql4_Poll_Collection, Mage_Reports_Model_Mysql4_Product_Downloads_Collection, Mage_Tag_Model_Mysql4_Customer_Collection, and Mage_Tag_Model_Mysql4_Tag_Collection.
Definition at line 307 of file Db.php.
00308 { 00309 $field = $this->_getMappedField($field); 00310 $this->_select->where($this->_getConditionSql($field, $condition)); 00311 return $this; 00312 }
addOrder | ( | $ | field, | |
$ | direction = self::SORT_ORDER_DESC | |||
) |
self.setOrder() alias
string | $field | |
string | $direction |
Definition at line 220 of file Db.php.
distinct | ( | $ | flag | ) |
Set select distinct
bool | $flag |
Reimplemented from Varien_Data_Collection.
Definition at line 628 of file Db.php.
getConnection | ( | ) |
getData | ( | ) |
Get all data array for collection
Definition at line 675 of file Db.php.
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 }
getIdFieldName | ( | ) |
getSelect | ( | ) |
getSelectCountSql | ( | ) |
Get SQL for get record count
Reimplemented in Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection, Mage_Customer_Model_Entity_Customer_Collection, Mage_Newsletter_Model_Mysql4_Subscriber_Collection, Mage_Reports_Model_Mysql4_Coupons_Collection, Mage_Reports_Model_Mysql4_Customer_Collection, Mage_Reports_Model_Mysql4_Invoiced_Collection, Mage_Reports_Model_Mysql4_Order_Collection, Mage_Reports_Model_Mysql4_Product_Collection, Mage_Reports_Model_Mysql4_Quote_Collection, Mage_Reports_Model_Mysql4_Refunded_Collection, Mage_Reports_Model_Mysql4_Review_Collection, Mage_Reports_Model_Mysql4_Review_Customer_Collection, Mage_Reports_Model_Mysql4_Tag_Customer_Collection, Mage_Reports_Model_Mysql4_Tax_Collection, Mage_Reports_Model_Mysql4_Wishlist_Product_Collection, Mage_Review_Model_Mysql4_Review_Product_Collection, Mage_Sales_Model_Mysql4_Order_Collection, Mage_Tag_Model_Mysql4_Customer_Collection, Mage_Tag_Model_Mysql4_Product_Collection, and Mage_Tag_Model_Mysql4_Tag_Collection.
Definition at line 172 of file Db.php.
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 }
getSelectSql | ( | $ | stringMode = false |
) |
getSize | ( | ) |
Get collection size
Reimplemented from Varien_Data_Collection.
Definition at line 158 of file Db.php.
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 }
initCache | ( | $ | object, | |
$ | idPrefix, | |||
$ | tags | |||
) |
load | ( | $ | printQuery = false , |
|
$ | logQuery = false | |||
) |
Load data
Reimplemented from Varien_Data_Collection.
Reimplemented in Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection, Mage_CatalogInventory_Model_Mysql4_Stock_Item_Collection, Mage_Core_Model_Mysql4_Collection_Abstract, Mage_Core_Model_Mysql4_Store_Collection, Mage_Core_Model_Mysql4_Store_Group_Collection, Mage_Core_Model_Mysql4_Website_Collection, Mage_Eav_Model_Entity_Collection_Abstract, Mage_GoogleBase_Model_Mysql4_Attribute_Collection, Mage_Log_Model_Mysql4_Visitor_Collection, Mage_Newsletter_Model_Mysql4_Problem_Collection, Mage_Newsletter_Model_Mysql4_Queue_Collection, Mage_Newsletter_Model_Mysql4_Subscriber_Collection, Mage_Review_Model_Mysql4_Review_Collection, Mage_Tag_Model_Entity_Customer_Collection, Mage_Tag_Model_Mysql4_Popular_Collection, Mage_Tag_Model_Mysql4_Tag_Collection, and Mage_Wishlist_Model_Mysql4_Item_Collection.
Definition at line 639 of file Db.php.
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 }
loadData | ( | $ | printQuery = false , |
|
$ | logQuery = false | |||
) |
Load data
Reimplemented from Varien_Data_Collection.
Definition at line 713 of file Db.php.
00714 { 00715 return $this->load($printQuery, $logQuery); 00716 }
printLogQuery | ( | $ | printQuery = false , |
|
$ | logQuery = false , |
|||
$ | sql = null | |||
) |
Print and/or log query
boolean | $printQuery | |
boolean | $logQuery |
Definition at line 725 of file Db.php.
00725 { 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 }
resetData | ( | ) |
Reset loaded for collection data array
Definition at line 702 of file Db.php.
setConnection | ( | $ | conn | ) |
Definition at line 123 of file Db.php.
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 }
setOrder | ( | $ | field, | |
$ | direction = self::SORT_ORDER_DESC | |||
) |
Add select order
string | $field | |
string | $direction |
Reimplemented from Varien_Data_Collection.
Reimplemented in Mage_CatalogSearch_Model_Mysql4_Fulltext_Collection, Mage_Eav_Model_Entity_Collection_Abstract, Mage_Reports_Model_Mysql4_Product_Collection, Mage_Reports_Model_Mysql4_Product_Downloads_Collection, Mage_Reports_Model_Mysql4_Review_Collection, Mage_Reports_Model_Mysql4_Tag_Customer_Collection, Mage_Reports_Model_Mysql4_Tag_Product_Collection, Mage_Reports_Model_Mysql4_Wishlist_Product_Collection, Mage_Review_Model_Mysql4_Review_Product_Collection, Mage_Tag_Model_Mysql4_Customer_Collection, and Mage_Tag_Model_Mysql4_Product_Collection.
Definition at line 208 of file Db.php.
unshiftOrder | ( | $ | field, | |
$ | direction = self::SORT_ORDER_DESC | |||
) |
Add select order to the beginning
string | $field | |
string | $direction |
Definition at line 232 of file Db.php.
$_idFieldName [protected] |
Reimplemented in Mage_Reports_Model_Mysql4_Product_Downloads_Collection.
$_map = null [protected] |
Reimplemented in Mage_Core_Model_Mysql4_Website_Collection, and Mage_Tag_Model_Mysql4_Tag_Collection.