Varien_Data_Collection_Db Class Reference

Inheritance diagram for Varien_Data_Collection_Db:

Varien_Data_Collection Mage_Admin_Model_Mysql4_Acl_Role_Collection Mage_Core_Model_Mysql4_Collection_Abstract Mage_Core_Model_Mysql4_Email_Template_Collection Mage_Directory_Model_Mysql4_Country_Collection Mage_Directory_Model_Mysql4_Currency_Collection Mage_Directory_Model_Mysql4_Region_Collection Mage_Eav_Model_Entity_Collection_Abstract Mage_Log_Model_Mysql4_Visitor_Collection Mage_Newsletter_Model_Mysql4_Subscriber_Collection Mage_Newsletter_Model_Mysql4_Template_Collection Mage_Rating_Model_Mysql4_Rating_Option_Collection Mage_Review_Model_Mysql4_Review_Collection Mage_Review_Model_Mysql4_Review_Status_Collection Mage_Review_Model_Mysql4_Review_Summary_Collection Mage_Shipping_Model_Mysql4_Carrier_Tablerate_Collection

List of all members.

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


Detailed Description

Definition at line 35 of file Db.php.


Constructor & Destructor Documentation

__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     }


Member Function Documentation

_afterLoad (  )  [protected]

_afterLoadData (  )  [protected]

Proces loaded collection data

Returns:
Varien_Data_Collection_Db

Reimplemented in Mage_Eav_Model_Mysql4_Entity_Attribute_Collection.

Definition at line 692 of file Db.php.

00693     {
00694         return $this;
00695     }

_canUseCache (  )  [protected]

Reimplemented in Mage_Core_Model_Mysql4_Collection_Abstract.

Definition at line 773 of file Db.php.

00774     {
00775         return false;
00776     }

_fetchAll ( select  )  [protected]

Fetch collection data

Parameters:
Zend_Db_Select $select
Returns:
array

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]

Retrieve cache instance

Returns:
Zend_Cache_Core

Definition at line 792 of file Db.php.

00793     {
00794         if (isset($this->_cacheConf['object'])) {
00795             return $this->_cacheConf['object'];
00796         }
00797         return false;
00798     }

_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]

Definition at line 468 of file Db.php.

00469     {
00470         return $fieldName;
00471     }

_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:

  • array("from"=>$fromValue, "to"=>$toValue)
  • array("like"=>$likeValue)
  • array("neq"=>$notEqualValue)
  • array("in"=>array($inValues))
  • array("nin"=>array($notInValues))

If non matched - sequential array is expected and OR conditions will be built using above mentioned structure

Parameters:
string $fieldName
integer|string|array $condition
Returns:
string

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

Parameters:
string 
Returns:
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]

Definition at line 333 of file Db.php.

00334     {
00335         if (isset($this->_map)) {
00336             return $this->_map;
00337         }
00338         else {
00339             return false;
00340         }
00341     }

_getSelectCacheId ( select  )  [protected]

Definition at line 778 of file Db.php.

00779     {
00780         $id = md5($select->__toString());
00781         if (isset($this->_cacheConf['prefix'])) {
00782             $id = $this->_cacheConf['prefix'].'_'.$id;
00783         }
00784         return $id;
00785     }

_renderFilters (  )  [protected]

Render sql select conditions

Returns:
Varien_Data_Collection_Db

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

Returns:
Varien_Data_Collection_Db

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:

  • array("from"=>$fromValue, "to"=>$toValue)
  • array("like"=>$likeValue)
  • array("neq"=>$notEqualValue)
  • array("in"=>array($inValues))
  • array("nin"=>array($notInValues))

If non matched - sequential array is expected and OR conditions will be built using above mentioned structure

Parameters:
string $fieldName
integer|string|array $condition
Returns:
string
protected function _getConditionSql($fieldName, $condition) { if (!is_array($condition)) { $condition = 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

Returns:
Varien_Data_Collection_Db

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

Returns:
Varien_Data_Collection_Db

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]

Definition at line 104 of file Db.php.

00105     {
00106         $this->_idFieldName = $fieldName;
00107         return $this;
00108     }

addBindParam ( name,
value 
)

Definition at line 88 of file Db.php.

00089     {
00090         $this->_bindParams[$name] = $value;
00091         return $this;
00092     }

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'), )

See also:
self._getConditionSql for $condition
Parameters:
string|array $attribute
null|string|array $condition
Returns:
Mage_Eav_Model_Entity_Collection_Abstract

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

Parameters:
string $field
string $direction
Returns:
Varien_Data_Collection_Db

Definition at line 220 of file Db.php.

00221     {
00222         return $this->_setOrder($field, $direction);
00223     }

distinct ( flag  ) 

Set select distinct

Parameters:
bool $flag

Reimplemented from Varien_Data_Collection.

Definition at line 628 of file Db.php.

00629     {
00630         $this->_select->distinct($flag);
00631         return $this;
00632     }

getConnection (  ) 

Retrieve connection object

Returns:
Zend_Db_Adapter_Abstract

Definition at line 148 of file Db.php.

00149     {
00150         return $this->_conn;
00151     }

getData (  ) 

Get all data array for collection

Returns:
array

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 (  ) 

Definition at line 110 of file Db.php.

00111     {
00112         return $this->_idFieldName;
00113     }

getSelect (  ) 

Get Zend_Db_Select instance

Returns:
Varien_Db_Select

Definition at line 138 of file Db.php.

00139     {
00140         return $this->_select;
00141     }

getSelectCountSql (  ) 

getSelectSql ( stringMode = false  ) 

Get sql select string or object

Parameters:
bool $stringMode
Returns:
string || Zend_Db_Select

Definition at line 193 of file Db.php.

00194     {
00195         if ($stringMode) {
00196             return $this->_select->__toString();
00197         }
00198         return $this->_select;
00199     }

getSize (  ) 

Get collection size

Returns:
int

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 
)

Definition at line 94 of file Db.php.

00095     {
00096         $this->_cacheConf = array(
00097             'object'    => $object,
00098             'prefix'    => $idPrefix,
00099             'tags'      => $tags
00100         );
00101         return $this;
00102     }

load ( printQuery = false,
logQuery = false 
)

Load data

Returns:
Varien_Data_Collection_Db

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

Returns:
Varien_Data_Collection

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

Parameters:
boolean $printQuery
boolean $logQuery
Returns:
Varien_Data_Collection_Db

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

Returns:
Varien_Data_Collection_Db

Definition at line 702 of file Db.php.

00703     {
00704         $this->_data = null;
00705         return $this;
00706     }

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 
)

unshiftOrder ( field,
direction = self::SORT_ORDER_DESC 
)

Add select order to the beginning

Parameters:
string $field
string $direction
Returns:
Varien_Data_Collection_Db

Definition at line 232 of file Db.php.

00233     {
00234         return $this->_setOrder($field, $direction, true);
00235     }


Member Data Documentation

$_bindParams = array() [protected]

Definition at line 62 of file Db.php.

$_cacheConf = null [protected]

Definition at line 51 of file Db.php.

$_conn [protected]

Definition at line 42 of file Db.php.

$_data = null [protected]

Definition at line 70 of file Db.php.

$_idFieldName [protected]

Reimplemented in Mage_Reports_Model_Mysql4_Product_Downloads_Collection.

Definition at line 60 of file Db.php.

$_map = null [protected]

$_select [protected]

Definition at line 49 of file Db.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:59 2009 for Magento by  doxygen 1.5.8