Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection Class Reference

Inheritance diagram for Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection:

Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract Mage_Eav_Model_Entity_Collection_Abstract Varien_Data_Collection_Db Varien_Data_Collection

List of all members.

Public Member Functions

 addIdFilter ($categoryIds)
 setLoadProductCount ($flag)
 setProductStoreId ($storeId)
 getProductStoreId ()
 load ($printQuery=false, $logQuery=false)
 loadProductCount ($items, $countRegular=true, $countAnchor=true)
 addPathFilter ($regexp)
 joinUrlRewrite ()
 addIsActiveFilter ()
 addNameToResult ()
 addUrlRewriteToResult ()
 addPathsFilter ($paths)
 addLevelFilter ($level)
 addOrderField ($field)

Protected Member Functions

 _construct ()
 _beforeLoad ()
 _afterLoad ()
 _loadProductCount ()

Protected Attributes

 $_eventPrefix = 'catalog_category_collection'
 $_eventObject = 'category_collection'
 $_productTable
 $_productStoreId
 $_productWebsiteTable
 $_loadWithProductCount = false


Detailed Description

Definition at line 35 of file Collection.php.


Member Function Documentation

_afterLoad (  )  [protected]

After collection load

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Reimplemented from Mage_Eav_Model_Entity_Collection_Abstract.

Definition at line 137 of file Collection.php.

00138     {
00139         Mage::dispatchEvent($this->_eventPrefix . '_load_after',
00140                             array($this->_eventObject => $this));
00141 
00142         return parent::_afterLoad();
00143     }

_beforeLoad (  )  [protected]

Before collection load

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Reimplemented from Mage_Eav_Model_Entity_Collection_Abstract.

Definition at line 125 of file Collection.php.

00126     {
00127         Mage::dispatchEvent($this->_eventPrefix . '_load_before',
00128                             array($this->_eventObject => $this));
00129         return parent::_beforeLoad();
00130     }

_construct (  )  [protected]

Init collection and determine table names

Todo:
Why 'core/resource' is used here ? What if catalog will use another resource ?

Reimplemented from Mage_Eav_Model_Entity_Collection_Abstract.

Definition at line 68 of file Collection.php.

00069     {
00070         $this->_init('catalog/category');
00071 
00072         /**
00073          * @todo Why 'core/resource' is used here ? What if catalog will use another resource ?
00074          */
00075         $this->_productWebsiteTable = Mage::getSingleton('core/resource')->getTableName('catalog/product_website');
00076         $this->_productTable = Mage::getSingleton('core/resource')->getTableName('catalog/category_product');
00077     }

_loadProductCount (  )  [protected]

Load categories product count

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 198 of file Collection.php.

00199     {
00200         $this->loadProductCount($this->_items, true, true);
00201     }

addIdFilter ( categoryIds  ) 

Enter description here...

Parameters:
array $categoryIds
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 85 of file Collection.php.

00086     {
00087         if (is_array($categoryIds)) {
00088             if (empty($categoryIds)) {
00089                 $condition = '';
00090             } else {
00091                 $condition = array('in' => $categoryIds);
00092             }
00093         } elseif (is_numeric($categoryIds)) {
00094             $condition = $categoryIds;
00095         } elseif (is_string($categoryIds)) {
00096             $ids = explode(',', $categoryIds);
00097             if (empty($ids)) {
00098                 $condition = $categoryIds;
00099             } else {
00100                 $condition = array('in' => $ids);
00101             }
00102         }
00103         $this->addFieldToFilter('entity_id', $condition);
00104         return $this;
00105     }

addIsActiveFilter (  ) 

Definition at line 296 of file Collection.php.

00297     {
00298         $this->addAttributeToFilter('is_active', 1);
00299         Mage::dispatchEvent($this->_eventPrefix . '_add_is_active_filter',
00300                             array($this->_eventObject => $this));
00301         return $this;
00302     }

addLevelFilter ( level  ) 

Definition at line 334 of file Collection.php.

00335     {
00336         $this->getSelect()->where('e.level <= ?', $level);
00337         return $this;
00338     }

addNameToResult (  ) 

Definition at line 304 of file Collection.php.

00305     {
00306         $this->addAttributeToSelect('name');
00307         return $this;
00308     }

addOrderField ( field  ) 

Definition at line 340 of file Collection.php.

00341     {
00342         $this->setOrder($field, 'ASC');
00343         return $this;
00344     }

addPathFilter ( regexp  ) 

Enter description here...

Parameters:
string $regexp
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 272 of file Collection.php.

00273     {
00274         $this->getSelect()->where(new Zend_Db_Expr("path regexp '{$regexp}'"));
00275         return $this;
00276     }

addPathsFilter ( paths  ) 

Definition at line 316 of file Collection.php.

00317     {
00318         if (!is_array($paths)) {
00319             $paths = array($paths);
00320         }
00321         $select = $this->getSelect();
00322         $orWhere = false;
00323         foreach ($paths as $path) {
00324             if ($orWhere) {
00325                 $select->orWhere('e.path LIKE ?', "$path%");
00326             } else {
00327                 $select->where('e.path LIKE ?', "$path%");
00328                 $orWhere = true;
00329             }
00330         }
00331         return $this;
00332     }

addUrlRewriteToResult (  ) 

Definition at line 310 of file Collection.php.

00311     {
00312         $this->joinUrlRewrite();
00313         return $this;
00314     }

getProductStoreId (  ) 

Get id of the store that we should count products on

Returns:
int

Definition at line 162 of file Collection.php.

00163     {
00164         if (is_null($this->_productStoreId)) {
00165             $this->_productStoreId = 0;
00166         }
00167         return $this->_productStoreId;
00168     }

joinUrlRewrite (  ) 

Joins url rewrite rules to collection

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 283 of file Collection.php.

00284     {
00285         $storeId = Mage::app()->getStore()->getId();
00286         $this->joinTable(
00287             'core/url_rewrite',
00288             'category_id=entity_id',
00289             array('request_path'),
00290             '{{table}}.is_system=1 AND {{table}}.product_id IS NULL AND {{table}}.store_id="'.$storeId.'" AND id_path LIKE "category/%"',
00291             'left'
00292         );
00293         return $this;
00294     }

load ( printQuery = false,
logQuery = false 
)

Enter description here...

Parameters:
boolean $printQuery
boolean $logQuery
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Reimplemented from Mage_Eav_Model_Entity_Collection_Abstract.

Definition at line 177 of file Collection.php.

00178     {
00179         if ($this->_loadWithProductCount) {
00180             $this->addAttributeToSelect('all_children');
00181             $this->addAttributeToSelect('is_anchor');
00182         }
00183 
00184         parent::load($printQuery, $logQuery);
00185 
00186         if ($this->_loadWithProductCount) {
00187             $this->_loadProductCount();
00188         }
00189 
00190         return $this;
00191     }

loadProductCount ( items,
countRegular = true,
countAnchor = true 
)

Load product count for specified items

Parameters:
array $items
boolean $countRegular get product count for regular (non-anchor) categories
boolean $countAnchor get product count for anchor categories
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 211 of file Collection.php.

00212     {
00213         $anchor     = array();
00214         $regular    = array();
00215 
00216         foreach ($items as $item) {
00217             if ($item->getIsAnchor()) {
00218                 $anchor[$item->getId()] = $item;
00219             } else {
00220                 $regular[$item->getId()] = $item;
00221             }
00222         }
00223 
00224         if ($countRegular) {
00225             // Retrieve regular categories product counts
00226             $regularIds = array_keys($regular);
00227             if (!empty($regularIds)) {
00228                 $select = $this->_conn->select();
00229                 $select->from(
00230                         array('main_table'=>$this->_productTable),
00231                         array('category_id', new Zend_Db_Expr('COUNT(main_table.product_id)'))
00232                     )
00233                     ->where($this->_conn->quoteInto('main_table.category_id IN(?)', $regularIds))
00234                     ->group('main_table.category_id');
00235                 $counts = $this->_conn->fetchPairs($select);
00236                 foreach ($regular as $item) {
00237                     if (isset($counts[$item->getId()])) {
00238                         $item->setProductCount($counts[$item->getId()]);
00239                     } else {
00240                         $item->setProductCount(0);
00241                     }
00242                 }
00243             }
00244         }
00245 
00246         if ($countAnchor) {
00247             // Retrieve Anchor categories product counts
00248             foreach ($anchor as $item) {
00249                 if ($allChildren = $item->getAllChildren()) {
00250                     $select = $this->_conn->select();
00251                     $select->from(
00252                             array('main_table' => $this->_productTable),
00253                             new Zend_Db_Expr('COUNT( DISTINCT main_table.product_id)')
00254                         )
00255                         ->joinInner(array('e' => $this->getTable('catalog/category')), 'main_table.category_id=e.entity_id', array())
00256                         ->where('e.entity_id=? OR ' . $this->_conn->quoteInto('e.path LIKE CONCAT(?)', array($item->getPath(), '/%')), $item->getId());
00257                     $item->setProductCount((int) $this->_conn->fetchOne($select));
00258                 } else {
00259                     $item->setProductCount(0);
00260                 }
00261             }
00262         }
00263         return $this;
00264     }

setLoadProductCount ( flag  ) 

Enter description here...

Parameters:
boolean $flag
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 113 of file Collection.php.

00114     {
00115         $this->_loadWithProductCount = $flag;
00116         return $this;
00117     }

setProductStoreId ( storeId  ) 

Set id of the store that we should count products on

Parameters:
int $storeId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection

Definition at line 151 of file Collection.php.

00152     {
00153         $this->_productStoreId = $storeId;
00154         return $this;
00155     }


Member Data Documentation

$_eventObject = 'category_collection' [protected]

Definition at line 49 of file Collection.php.

$_eventPrefix = 'catalog_category_collection' [protected]

Definition at line 42 of file Collection.php.

$_loadWithProductCount = false [protected]

Definition at line 62 of file Collection.php.

$_productStoreId [protected]

Definition at line 58 of file Collection.php.

$_productTable [protected]

Definition at line 51 of file Collection.php.

$_productWebsiteTable [protected]

Definition at line 60 of file Collection.php.


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

Generated on Sat Jul 4 17:23:43 2009 for Magento by  doxygen 1.5.8