Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat Class Reference

Inheritance diagram for Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 setStoreId ($storeId)
 getStoreId ()
 getMainTable ()
 getMainStoreTable ($storeId=0)
 getUseStoreTables ()
 addInactiveCategoryIds ($ids)
 getInactiveCategoryIds ()
 addChildNodes ($children, $path, $parent)
 getNodes ($parentId, $recursionLevel=0, $storeId=0)
 getCategories ($parent, $recursionLevel=0, $sorted=false, $asCollection=false, $toLoad=true)
 getNodeById ($nodeId, $nodes=null)
 isRebuilt ()
 rebuild ()
 createTable ($stores)
 deleteStores ($stores)
 synchronize ($category=null, $storeIds=array())
 removeStores ($stores)
 move ($categoryId, $prevParentId, $parentId)
 getChildrenAmount ($category, $isActiveFlag=true)
 getProductCount ($category)
 getParentCategories ($category, $isActive=true)
 getChildrenCategories ($category)
 isInRootCategoryList ($category)
 getChildren ($category, $recursive=true, $isActive=true)
 getAllChildren ($category)
 checkId ($id)
 getDesignUpdateData ($category)
 getAnchorsAbove (array $filterIds, $storeId=0)

Protected Member Functions

 _construct ()
 _initInactiveCategoryIds ()
 _loadNodes ($parentNode=null, $recursionLevel=0, $storeId=0)
 _getTableSqlSchema ($storeId=0)
 _createTable ($stores)
 _deleteTable ($stores)
 _synchronize ($category, $action=null)
 _move ($prevParentPath, $parentPath)
 _prepareDataForAllFields ($category, $replaceFields=array())

Protected Attributes

 $_storeId = null
 $_loaded = false
 $_nodes = array()
 $_inactiveCategoryIds = null
 $_isRebuilt = null


Detailed Description

Definition at line 35 of file Flat.php.


Member Function Documentation

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 52 of file Flat.php.

00053     {
00054         $this->_init('catalog/category_flat', 'entity_id');
00055     }

_createTable ( stores  )  [protected]

Creating table and adding attributes as fields to table

Parameters:
array|integer $stores
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 451 of file Flat.php.

00452     {
00453         if (!is_array($stores)) {
00454             $stores = (int) $stores;
00455         }
00456         $_read  = $this->_getReadAdapter();
00457         $_write = $this->_getWriteAdapter();
00458         if ($this->getUseStoreTables() && is_array($stores)) {
00459             foreach ($stores as $store) {
00460                 $_read->query("DROP TABLE IF EXISTS `{$this->getMainStoreTable($store)}`");
00461                 $_read->query($this->_getTableSqlSchema($store));
00462             }
00463         } else {
00464             $_read->query("DROP TABLE IF EXISTS `{$this->getMainStoreTable($stores)}`");
00465             $_read->query($this->_getTableSqlSchema($stores));
00466         }
00467         $selectAttribute = $_read->select()
00468             ->from($this->getTable('eav/entity_type'), array())
00469             ->join(
00470                 $this->getTable('eav/attribute'),
00471                 $this->getTable('eav/attribute').'.entity_type_id = '.$this->getTable('eav/entity_type').'.entity_type_id',
00472                 $this->getTable('eav/attribute').'.*'
00473             )
00474             ->where($this->getTable('eav/entity_type').'.entity_type_code=?', 'catalog_category');
00475         $resultAttribute = $_read->fetchAll($selectAttribute);
00476         foreach ($resultAttribute as $attribute) {
00477             $type = '';
00478             switch ($attribute['backend_type']) {
00479                 case 'varchar':
00480                     $type = 'varchar(255) not null default \'\'';
00481                     break;
00482                 case 'int':
00483                     $type = 'int(10) not null default \'0\'';
00484                     break;
00485                 case 'text':
00486                     $type = 'text';
00487                     break;
00488                 case 'datetime':
00489                     $type = 'datetime not null default \'0000-00-00 00:00:00\'';
00490                     break;
00491                 case 'decimal':
00492                     $type = 'decimal(10,2) not null default \'0.00\'';
00493                     break;
00494             }
00495             if ($type) {
00496                 if ($this->getUseStoreTables() && is_array($stores)) {
00497                     foreach ($stores as $store) {
00498                         $_write->addColumn($this->getMainStoreTable($store), $attribute['attribute_code'], $type);
00499                     }
00500                 } else {
00501                     $_write->addColumn($this->getMainStoreTable($stores), $attribute['attribute_code'], $type);
00502                 }
00503             }
00504         }
00505         return $this;
00506     }

_deleteTable ( stores  )  [protected]

Delete table(s) of given stores.

Parameters:
array|integer $stores
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 526 of file Flat.php.

00527     {
00528         if (!is_array($stores)) {
00529             $stores = array($stores);
00530         }
00531         foreach ($stores as $store) {
00532             $_tableExist = $this->_getReadAdapter()->query(
00533                 "DROP TABLE IF EXISTS `{$this->getMainStoreTable($store)}`"
00534             );
00535         }
00536         return $this;
00537     }

_getTableSqlSchema ( storeId = 0  )  [protected]

Definition at line 372 of file Flat.php.

00373     {
00374         $storeId = Mage::app()->getStore($storeId)->getId();
00375         $schema = "CREATE TABLE `{$this->getMainStoreTable($storeId)}` (
00376                 `entity_id` int(10) unsigned not null,
00377                 `store_id` smallint(5) unsigned not null default '0',
00378                 `parent_id` int(10) unsigned not null default '0',
00379                 `path` varchar(255) not null default '',
00380                 `level` int(11) not null default '0',
00381                 `position` int(11) not null default '0',
00382                 `children_count` int(11) not null,
00383                 `created_at` datetime not null default '0000-00-00 00:00:00',
00384                 `updated_at` datetime not null default '0000-00-00 00:00:00',
00385                 KEY `CATEGORY_FLAT_CATEGORY_ID` (`entity_id`),
00386                 KEY `CATEGORY_FLAT_STORE_ID` (`store_id`),
00387                 KEY `path` (`path`),
00388                 KEY `IDX_LEVEL` (`level`),
00389                 CONSTRAINT `FK_CATEGORY_FLAT_CATEGORY_ID_STORE_{$storeId}` FOREIGN KEY (`entity_id`)
00390                     REFERENCES `{$this->getTable('catalog/category')}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00391                 CONSTRAINT `FK_CATEGORY_FLAT_STORE_ID_STORE_{$storeId}` FOREIGN KEY (`store_id`)
00392                     REFERENCES `{$this->getTable('core/store')}` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
00393             ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
00394         return $schema;
00395     }

_initInactiveCategoryIds (  )  [protected]

Retreive inactive categories ids

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 139 of file Flat.php.

00140     {
00141         $this->_inactiveCategoryIds = array();
00142         Mage::dispatchEvent('catalog_category_tree_init_inactive_category_ids', array('tree'=>$this));
00143         return $this;
00144     }

_loadNodes ( parentNode = null,
recursionLevel = 0,
storeId = 0 
) [protected]

Load nodes by parent id

Parameters:
integer $parentId
integer $recursionLevel
integer $storeId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 168 of file Flat.php.

00169     {
00170         $_conn = $this->_getReadAdapter();
00171         $startLevel = 1;
00172         $parentPath = '';
00173         if ($parentNode instanceof Mage_Catalog_Model_Category) {
00174             $parentPath = $parentNode->getPath();
00175             $startLevel = $parentNode->getLevel();
00176         } elseif (is_numeric($parentNode)) {
00177             $selectParent = $_conn->select()
00178                 ->from($this->getMainStoreTable())
00179                 ->where('entity_id = ?', $parentNode)
00180                 ->where('store_id = ?', '0');
00181             if ($parentNode = $_conn->fetchRow($selectParent)) {
00182                 $parentPath = $parentNode['path'];
00183                 $startLevel = $parentNode['level'];
00184             }
00185         }
00186         $select = $_conn->select()
00187             ->from(array('main_table'=>$this->getMainStoreTable($storeId)), array('main_table.entity_id', 'main_table.name', 'main_table.path', 'main_table.is_active', 'main_table.is_anchor'))
00188             ->joinLeft(
00189                 array('url_rewrite'=>$this->getTable('core/url_rewrite')),
00190                 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND url_rewrite.product_id IS NULL AND url_rewrite.store_id="'.$storeId.'" AND url_rewrite.id_path LIKE "category/%"',
00191                 array('request_path' => 'url_rewrite.request_path'))
00192             ->where('main_table.is_active = ?', '1')
00193 //            ->order('main_table.path', 'ASC')
00194             ->order('main_table.position', 'ASC');
00195 
00196 
00197 
00198         if ($parentPath) {
00199             $select->where($_conn->quoteInto("main_table.path like ?", "$parentPath/%"));
00200         }
00201         if ($recursionLevel != 0) {
00202             $select->where("main_table.level <= ?", $startLevel + $recursionLevel);
00203         }
00204 
00205         $inactiveCategories = $this->getInactiveCategoryIds();
00206 
00207         if (!empty($inactiveCategories)) {
00208             $select->where('main_table.entity_id NOT IN (?)', $inactiveCategories);
00209         }
00210 
00211         $arrNodes = $_conn->fetchAll($select);
00212         $nodes = array();
00213         foreach ($arrNodes as $node) {
00214             $node['id'] = $node['entity_id'];
00215             $nodes[$node['id']] = Mage::getModel('catalog/category')->setData($node);
00216         }
00217 
00218         return $nodes;
00219     }

_move ( prevParentPath,
parentPath 
) [protected]

Synchronize flat data with eav model when category was moved.

Parameters:
string $prevParentPath
string $parentPath
array $storeIds
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 584 of file Flat.php.

00585     {
00586         $_staticFields = array(
00587             'parent_id',
00588             'path',
00589             'level',
00590             'position',
00591             'children_count',
00592             'updated_at'
00593         );
00594         $select = $this->_getReadAdapter()->select()
00595             ->from($this->getTable('core/store'), 'store_id');
00596         $stores = $this->_getReadAdapter()->fetchAll($select);
00597         foreach ($stores as $store) {
00598             $update = "UPDATE {$this->getMainStoreTable($store['store_id'])}, {$this->getTable('catalog/category')} SET";
00599             foreach ($_staticFields as $field) {
00600                 $update .= " {$this->getMainStoreTable($store['store_id'])}.".$field."={$this->getTable('catalog/category')}.".$field.",";
00601             }
00602             $update = substr($update, 0, -1);
00603             $update .= " WHERE {$this->getMainStoreTable($store['store_id'])}.entity_id = {$this->getTable('catalog/category')}.entity_id AND " .
00604                 "({$this->getTable('catalog/category')}.path like '$parentPath/%' OR " .
00605                 "{$this->getTable('catalog/category')}.path like '$prevParentPath/%')";
00606             $this->_getWriteAdapter()->query($update);
00607         }
00608         return $this;
00609     }

_prepareDataForAllFields ( category,
replaceFields = array() 
) [protected]

Prepare array of category data to insert or update.

array( 'field_name' => 'value' )

Parameters:
Mage_Catalog_Model_Category $category
array $replaceFields
Returns:
array

Definition at line 758 of file Flat.php.

00759     {
00760         $table = $this->_getReadAdapter()->describeTable($this->getMainStoreTable($category->getStoreId()));
00761         $data = array();
00762         foreach ($table as $column=>$columnData) {
00763             if (null !== $category->getData($column)) {
00764                 if (key_exists($column, $replaceFields)) {
00765                     $value = $category->getData($replaceFields[$column]);
00766                 } else {
00767                     $value = $category->getData($column);
00768                 }
00769                 if (is_array($value)) {
00770                     $value = implode(',', $value);
00771                 }
00772                 $data[$column] = $value;
00773             }
00774         }
00775         return $data;
00776     }

_synchronize ( category,
action = null 
) [protected]

Synchronize flat data with eav model.

Parameters:
Mage_Catalog_Model_Category $category
null|string $action
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 546 of file Flat.php.

00547     {
00548         if (is_null($action)) {
00549             $select = $this->_getReadAdapter()->select()
00550                 ->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')
00551                 ->where('entity_id = ?', $category->getId());
00552             if ($result = $this->_getReadAdapter()->fetchOne($select)) {
00553                 $action = 'update';
00554             } else {
00555                 $action = 'insert';
00556             }
00557         }
00558 
00559         if ($action == 'update') {
00560             // update
00561             $this->_getWriteAdapter()->update(
00562                 $this->getMainStoreTable($category->getStoreId()),
00563                 $this->_prepareDataForAllFields($category),
00564                 $this->_getReadAdapter()->quoteInto('entity_id = ?', $category->getId())
00565             );
00566         } elseif ($action == 'insert') {
00567             // insert
00568             $this->_getWriteAdapter()->insert(
00569                 $this->getMainStoreTable($category->getStoreId()),
00570                 $this->_prepareDataForAllFields($category)
00571             );
00572         }
00573         return $this;
00574     }

addChildNodes ( children,
path,
parent 
)

Creating sorted array of nodes

Parameters:
array $children
string $path
Varien_Object $parent

Definition at line 228 of file Flat.php.

00229     {
00230         if (isset($children[$path])) {
00231             foreach ($children[$path] as $child) {
00232                 $childrenNodes = $parent->getChildrenNodes();
00233                 if ($childrenNodes && isset($childrenNodes[$child->getId()])) {
00234                     $childrenNodes[$child['entity_id']]->setChildrenNodes(array($child->getId()=>$child));
00235                 } else {
00236                     if ($childrenNodes) {
00237                         $childrenNodes[$child->getId()] = $child;
00238                     } else {
00239                         $childrenNodes = array($child->getId()=>$child);
00240                     }
00241                     $parent->setChildrenNodes($childrenNodes);
00242                 }
00243 
00244                 if ($path) {
00245                     $childrenPath = explode('/', $path);
00246                 } else {
00247                     $childrenPath = array();
00248                 }
00249                 $childrenPath[] = $child->getId();
00250                 $childrenPath = implode('/', $childrenPath);
00251                 $this->addChildNodes($children, $childrenPath, $child);
00252             }
00253         }
00254     }

addInactiveCategoryIds ( ids  ) 

Add inactive categories ids

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 125 of file Flat.php.

00126     {
00127         if (!is_array($this->_inactiveCategoryIds)) {
00128             $this->_initInactiveCategoryIds();
00129         }
00130         $this->_inactiveCategoryIds = array_merge($ids, $this->_inactiveCategoryIds);
00131         return $this;
00132     }

checkId ( id  ) 

Check if category id exist

Parameters:
int $id
Returns:
bool

Definition at line 919 of file Flat.php.

00920     {
00921         $select = $this->_getReadAdapter()->select()
00922             ->from($this->getMainStoreTable(), 'entity_id')
00923             ->where('entity_id=?', $id);
00924         return $this->_getReadAdapter()->fetchOne($select);
00925     }

createTable ( stores  ) 

Create Flate Table(s)

Parameters:
array|int $stores
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 440 of file Flat.php.

00441     {
00442         return $this->_createTable($stores);
00443     }

deleteStores ( stores  ) 

Delete store table(s) of given stores;

Parameters:
array|integer $stores
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 514 of file Flat.php.

00515     {
00516         $this->_deleteTable($stores);
00517         return $this;
00518     }

getAllChildren ( category  ) 

Return all children ids of category (with category id)

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 904 of file Flat.php.

00905     {
00906         $categoriesIds = $this->getChildren($category);
00907         $myId = array($category->getId());
00908         $categoriesIds = array_merge($myId, $categoriesIds);
00909 
00910         return $categoriesIds;
00911     }

getAnchorsAbove ( array filterIds,
storeId = 0 
)

Retrieve anchors above

Parameters:
array $filterIds
int $storeId
Returns:
array

Definition at line 973 of file Flat.php.

00974     {
00975         $select = $this->_getReadAdapter()->select()
00976             ->from(array('e' => $this->getMainStoreTable($storeId)), 'entity_id')
00977             ->where('is_anchor = ?', 1)
00978             ->where('entity_id IN (?)', $filterIds);
00979 
00980         return $this->_getReadAdapter()->fetchCol($select);
00981     }

getCategories ( parent,
recursionLevel = 0,
sorted = false,
asCollection = false,
toLoad = true 
)

Return array or collection of categories

Parameters:
integer $parent
integer $recursionLevel
boolean|string $sorted
boolean $asCollection
boolean $toLoad
Returns:
array|Varien_Data_Collection

Definition at line 306 of file Flat.php.

00307     {
00308         if ($asCollection) {
00309             $parentPath = $this->_getReadAdapter()->fetchOne(new Zend_Db_Expr("
00310                 SELECT path FROM {$this->getMainStoreTable()} WHERE entity_id = {$parent}
00311             "));
00312             $collection = Mage::getModel('catalog/category')->getCollection()
00313                 ->addNameToResult()
00314                 ->addUrlRewriteToResult()
00315                 ->addParentPathFilter($parentPath)
00316                 ->addStoreFilter()
00317                 ->addSortedField($sorted);
00318             if ($toLoad) {
00319                 return $collection->load();
00320             }
00321             return $collection;
00322         }
00323         return $this->getNodes($parent, $recursionLevel, Mage::app()->getStore()->getId());
00324     }

getChildren ( category,
recursive = true,
isActive = true 
)

Return children ids of category

Parameters:
Mage_Catalog_Model_Category $category
integer $level
Returns:
array

Definition at line 879 of file Flat.php.

00880     {
00881         $select = $this->_getReadAdapter()->select()
00882             ->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')
00883             ->where('path LIKE ?', "{$category->getPath()}/%");
00884         if (!$recursive) {
00885             $select->where('level <= ?', $category->getLevel() + 1);
00886         }
00887         if ($isActive) {
00888             $select->where('is_active = ?', '1');
00889         }
00890         $_categories = $this->_getReadAdapter()->fetchAll($select);
00891         $categoriesIds = array();
00892         foreach ($_categories as $_category) {
00893             $categoriesIds[] = $_category['entity_id'];
00894         }
00895         return $categoriesIds;
00896     }

getChildrenAmount ( category,
isActiveFlag = true 
)

Get count of active/not active children categories

Parameters:
Mage_Catalog_Model_Category $category
bool $isActiveFlag
Returns:
integer

Definition at line 785 of file Flat.php.

00786     {
00787         $_table = $this->getMainStoreTable($category->getStoreId());
00788         $select = $this->_getReadAdapter()->select()
00789             ->from($_table, "COUNT({$_table}.entity_id)")
00790             ->where("{$_table}.path LIKE ?", $category->getPath() . '/%')
00791             ->where("{$_table}.is_active = ?", (int) $isActiveFlag);
00792         return (int) $this->_getReadAdapter()->fetchOne($select);
00793     }

getChildrenCategories ( category  ) 

Return children categories of category

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 844 of file Flat.php.

00845     {
00846 //        $node = $this->getNodeById($category->getId());
00847 //        if ($node && $node->getChildrenNodes()) {
00848 //            return $node->getChildrenNodes();
00849 //        }
00850         $categories = $this->_loadNodes($category, 1, $category->getStoreId());
00851         return $categories;
00852     }

getDesignUpdateData ( category  ) 

Get design update data of parent categories

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 933 of file Flat.php.

00934     {
00935         $categories = array();
00936         $pathIds = array();
00937         foreach (array_reverse($category->getParentIds()) as $pathId) {
00938             if ($pathId == Mage::app()->getStore()->getRootCategoryId()) {
00939                 $pathIds[] = $pathId;
00940                 break;
00941             }
00942             $pathIds[] = $pathId;
00943         }
00944         $select = $this->_getReadAdapter()->select()
00945             ->from(
00946                 array('main_table' => $this->getMainStoreTable($category->getStoreId())),
00947                 array(
00948                     'main_table.entity_id',
00949                     'main_table.custom_design',
00950                     'main_table.custom_design_apply',
00951                     'main_table.custom_design_from',
00952                     'main_table.custom_design_to',
00953                 )
00954             )
00955             ->where('main_table.entity_id IN (?)', $pathIds)
00956             ->where('main_table.is_active = ?', '1')
00957             ->order('main_table.path DESC');
00958         $result = $this->_getReadAdapter()->fetchAll($select);
00959         foreach ($result as $row) {
00960             $row['id'] = $row['entity_id'];
00961             $categories[$row['entity_id']] = Mage::getModel('catalog/category')->setData($row);
00962         }
00963         return $categories;
00964     }

getInactiveCategoryIds (  ) 

Retreive inactive categories ids

Returns:
array

Definition at line 151 of file Flat.php.

00152     {
00153         if (!is_array($this->_inactiveCategoryIds)) {
00154             $this->_initInactiveCategoryIds();
00155         }
00156 
00157         return $this->_inactiveCategoryIds;
00158     }

getMainStoreTable ( storeId = 0  ) 

Return name of table for given $storeId.

Parameters:
integer $storeId
Returns:
string

Definition at line 98 of file Flat.php.

00099     {
00100         $table = parent::getMainTable();
00101         if (is_string($storeId)) {
00102             $storeId = intval($storeId);
00103         }
00104         if ($this->getUseStoreTables() && $storeId) {
00105             $table .= '_store_'.$storeId;
00106         }
00107         return $table;
00108     }

getMainTable (  ) 

Get main table name

Returns:
string

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 87 of file Flat.php.

00088     {
00089         return $this->getMainStoreTable($this->getStoreId());
00090     }

getNodeById ( nodeId,
nodes = null 
)

Return node with id $nodeId

Parameters:
integer $nodeId
array $nodes
Returns:
Varien_Object

Definition at line 333 of file Flat.php.

00334     {
00335         if (is_null($nodes)) {
00336             $nodes = $this->getNodes();
00337         }
00338         if (isset($nodes[$nodeId])) {
00339             return $nodes[$nodeId];
00340         }
00341         foreach ($nodes as $node) {
00342 //            if ($node->getId() == $nodeId) {
00343 //                return $node;
00344 //            }
00345             if ($node->getChildrenNodes()) {
00346                 return $this->getNodeById($nodeId, $node->getChildrenNodes());
00347             }
00348         }
00349         return array();
00350     }

getNodes ( parentId,
recursionLevel = 0,
storeId = 0 
)

Return sorted array of nodes

Parameters:
integer|null $parentId
integer $recursionLevel
integer $storeId
Returns:
array

Definition at line 264 of file Flat.php.

00265     {
00266         if (!$this->_loaded) {
00267             $selectParent = $this->_getReadAdapter()->select()
00268                 ->from($this->getMainStoreTable())
00269                 ->where('entity_id = ?', $parentId);
00270             if ($parentNode = $this->_getReadAdapter()->fetchRow($selectParent)) {
00271                 $parentNode['id'] = $parentNode['entity_id'];
00272                 $parentNode = Mage::getModel('catalog/category')->setData($parentNode);
00273                 $this->_nodes[$parentNode->getId()] = $parentNode;
00274                 $nodes = $this->_loadNodes($parentNode, $recursionLevel, $storeId);
00275                 $childrenItems = array();
00276                 foreach ($nodes as $node) {
00277                     $pathToParent = explode('/', $node->getPath());
00278                     array_pop($pathToParent);
00279                     $pathToParent = implode('/', $pathToParent);
00280                     $childrenItems[$pathToParent][] = $node;
00281                 }
00282                 $this->addChildNodes($childrenItems, $parentNode->getPath(), $parentNode);
00283                 $childrenNodes = $this->_nodes[$parentNode->getId()];
00284                 if ($childrenNodes->getChildrenNodes()) {
00285                     $this->_nodes = $childrenNodes->getChildrenNodes();
00286                 }
00287                 else {
00288                     $this->_nodes = array();
00289                 }
00290                 $this->_loaded = true;
00291             }
00292         }
00293         return $this->_nodes;
00294     }

getParentCategories ( category,
isActive = true 
)

Return parent categories of category

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 816 of file Flat.php.

00817     {
00818         $categories = array();
00819         $select = $this->_getReadAdapter()->select()
00820             ->from(array('main_table' => $this->getMainStoreTable($category->getStoreId())), array('main_table.entity_id', 'main_table.name'))
00821             ->joinLeft(
00822                 array('url_rewrite'=>$this->getTable('core/url_rewrite')),
00823                 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND url_rewrite.product_id IS NULL AND url_rewrite.store_id="'.$category->getStoreId().'" AND url_rewrite.id_path LIKE "category/%"',
00824                 array('request_path' => 'url_rewrite.request_path'))
00825             ->where('main_table.entity_id IN (?)', array_reverse(explode(',', $category->getPathInStore())));
00826         if ($isActive) {
00827             $select->where('main_table.is_active = ?', '1');
00828         }
00829         $select->order('main_table.path ASC');
00830         $result = $this->_getReadAdapter()->fetchAll($select);
00831         foreach ($result as $row) {
00832             $row['id'] = $row['entity_id'];
00833             $categories[$row['entity_id']] = Mage::getModel('catalog/category')->setData($row);
00834         }
00835         return $categories;
00836     }

getProductCount ( category  ) 

Get products count in category

Parameters:
Mage_Catalog_Model_Category $category
Returns:
integer

Definition at line 801 of file Flat.php.

00802     {
00803         $select =  $this->_getReadAdapter()->select()
00804             ->from($this->getTable('catalog/category_product'), "COUNT({$this->getTable('catalog/category_product')}.product_id)")
00805             ->where("{$this->getTable('catalog/category_product')}.category_id = ?", $category->getId())
00806             ->group("{$this->getTable('catalog/category_product')}.category_id");
00807         return (int) $this->_getReadAdapter()->fetchOne($select);
00808     }

getStoreId (  ) 

Return store id

Returns:
integer

Definition at line 74 of file Flat.php.

00075     {
00076         if (is_null($this->_storeId)) {
00077             return Mage::app()->getStore()->getId();
00078         }
00079         return $this->_storeId;
00080     }

getUseStoreTables (  ) 

Return true if need use for each store different table of flat categoris data.

Returns:
boolean

Definition at line 115 of file Flat.php.

00116     {
00117         return true;
00118     }

isInRootCategoryList ( category  ) 

Check is category in list of store categories

Parameters:
Mage_Catalog_Model_Category $category
Returns:
boolean

Definition at line 860 of file Flat.php.

00861     {
00862         $innerSelect = $this->_getReadAdapter()->select()
00863             ->from($this->getMainStoreTable($category->getStoreId()), new Zend_Db_Expr("CONCAT(path, '/%')"))
00864             ->where('entity_id = ?', Mage::app()->getStore()->getRootCategoryId());
00865         $select = $this->_getReadAdapter()->select()
00866             ->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')
00867             ->where('entity_id = ?', $category->getId())
00868             ->where(new Zend_Db_Expr("path LIKE ({$innerSelect->__toString()})"));
00869         return (bool) $this->_getReadAdapter()->fetchOne($select);
00870     }

isRebuilt (  ) 

Check if category flat data is rebuilt

Returns:
bool

Definition at line 357 of file Flat.php.

00358     {
00359         if ($this->_isRebuilt === null) {
00360             $select = $this->_getReadAdapter()->select()
00361                 ->from($this->getMainStoreTable($this->getStoreId()), 'entity_id')
00362                 ->limit(1);
00363             try {
00364                 $this->_isRebuilt = (bool) $this->_getReadAdapter()->fetchOne($select);
00365             } catch (Exception $e) {
00366                 $this->_isRebuilt = false;
00367             }
00368         }
00369         return $this->_isRebuilt;
00370     }

move ( categoryId,
prevParentId,
parentId 
)

Synchronize flat data with eav after moving category

Parameters:
integer $categoryId
integer $prevParentId
integer $parentId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 687 of file Flat.php.

00688     {
00689         $_staticFields = array(
00690             'parent_id',
00691             'path',
00692             'level',
00693             'position',
00694             'children_count',
00695             'updated_at'
00696         );
00697         $prevParent = Mage::getModel('catalog/category')->load($prevParentId);
00698         $parent = Mage::getModel('catalog/category')->load($parentId);
00699         if ($prevParent->getStore()->getWebsiteId() != $parent->getStore()->getWebsiteId()) {
00700             foreach ($prevParent->getStoreIds() as $storeId) {
00701                 $this->_getWriteAdapter()->delete(
00702                     $this->getMainStoreTable($storeId),
00703                     $this->_getReadAdapter()->quoteInto('entity_id = ?', $categoryId)
00704                 );
00705             }
00706             $categoryPath = $this->_getReadAdapter()->fetchOne("
00707                 SELECT
00708                     path
00709                 FROM
00710                     {$this->getTable('catalog/category')}
00711                 WHERE
00712                     entity_id = '$categoryId'
00713             ");
00714             $select = $this->_getReadAdapter()->select()
00715                 ->from($this->getTable('catalog/category'), 'entity_id')
00716                 ->where('path LIKE ?', "$categoryPath/%")
00717                 ->orWhere('path = ?', $categoryPath);
00718             $_categories = $this->_getReadAdapter()->fetchAll($select);
00719             foreach ($_categories as $_category) {
00720                 foreach ($parent->getStoreIds() as $storeId) {
00721                     $_tmpCategory = Mage::getModel('catalog/category')
00722                         ->setStoreId($storeId)
00723                         ->load($_category['entity_id']);
00724                     $this->_synchronize($_tmpCategory);
00725                 }
00726             }
00727         } else {
00728             foreach ($parent->getStoreIds() as $store) {
00729                 $update = "UPDATE {$this->getMainStoreTable($store)}, {$this->getTable('catalog/category')} SET";
00730                 foreach ($_staticFields as $field) {
00731                     $update .= " {$this->getMainStoreTable($store)}.".$field."={$this->getTable('catalog/category')}.".$field.",";
00732                 }
00733                 $update = substr($update, 0, -1);
00734                 $update .= " WHERE {$this->getMainStoreTable($store)}.entity_id = {$this->getTable('catalog/category')}.entity_id AND " .
00735                     "({$this->getTable('catalog/category')}.path like '{$parent->getPath()}/%' OR " .
00736                     "{$this->getTable('catalog/category')}.path like '{$prevParent->getPath()}/%')";
00737                 $this->_getWriteAdapter()->query($update);
00738             }
00739         }
00740         $prevParent = null;
00741         $parent = null;
00742         $_tmpCategory = null;
00743 //        $this->_move($categoryId, $prevParentPath, $parentPath);
00744         return $this;
00745     }

rebuild (  ) 

Rebuild flat data from eav

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 402 of file Flat.php.

00403     {
00404         $_read  = $this->_getReadAdapter();
00405         if ($this->getUseStoreTables()) {
00406             $stores = array();
00407             $selectStores = $_read->select()
00408                 ->from($this->getTable('core/store'), 'store_id');
00409             $stores = array();
00410             foreach ($_read->fetchAll($selectStores) as $store) {
00411                 $stores[] = $store['store_id'];
00412             }
00413             $this->_createTable($stores);
00414         } else {
00415             $this->_createTable(0);
00416         }
00417         $select = $this->_getReadAdapter()->select()
00418             ->from($this->getTable('catalog/category'), 'entity_id');
00419         $_categories = $this->_getReadAdapter()->fetchAll($select);
00420         foreach ($_categories as $_category) {
00421             foreach ($stores as $store) {
00422                 $_tmpCategory = Mage::getModel('catalog/category')
00423                     ->setStoreId($store)
00424                     ->load($_category['entity_id']);
00425                 if ($_tmpCategory->getId()) {
00426                     $this->_synchronize($_tmpCategory, 'insert');
00427                 }
00428             }
00429         }
00430         $_tmpCategory = null;
00431         return $this;
00432     }

removeStores ( stores  ) 

Definition at line 673 of file Flat.php.

00674     {
00675         $this->_deleteTable($stores);
00676         return $this;
00677     }

setStoreId ( storeId  ) 

Set store id

Parameters:
integer $storeId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 63 of file Flat.php.

00064     {
00065         $this->_storeId = $storeId;
00066         return $this;
00067     }

synchronize ( category = null,
storeIds = array() 
)

Synchronize flat data with eav model.

Parameters:
Mage_Catalog_Model_Category|array $category
array $storeIds
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat

Definition at line 618 of file Flat.php.

00619     {
00620         if (is_null($category)) {
00621             $storesCondition = '';
00622             if (!empty($storeIds)) {
00623                 $storesCondition = $this->_getReadAdapter()->quoteInto(
00624                     ' AND s.store_id IN (?)', $storeIds
00625                 );
00626             }
00627             $stores = $this->_getReadAdapter()->fetchAll("
00628                 SELECT
00629                     s.store_id, s.website_id, c.path AS root_path
00630                 FROM
00631                     {$this->getTable('core/store')} AS s,
00632                     {$this->getTable('core/store_group')} AS sg,
00633                     {$this->getTable('catalog/category')} AS c
00634                 WHERE
00635                     sg.group_id=s.group_id
00636                     AND c.entity_id=sg.root_category_id
00637                     {$storesCondition}
00638             ");
00639             foreach ($stores as $store) {
00640                 $select = $this->_getReadAdapter()->select()
00641                     ->from($this->getTable('catalog/category'), 'entity_id')
00642                     ->where('path LIKE ?', "{$store['root_path']}/%")
00643                     ->orWhere('path = ?', $store['root_path']);
00644                 $_categories = $this->_getReadAdapter()->fetchAll($select);
00645                 if (!$this->_getReadAdapter()->showTableStatus($this->getMainStoreTable($store['store_id']))) {
00646                     $this->_createTable($store['store_id']);
00647                 }
00648                 $this->_getWriteAdapter()->delete(
00649                     $this->getMainStoreTable($store['store_id']),
00650                     $this->_getReadAdapter()->quoteInto('store_id = ?', $store['store_id'])
00651                 );
00652                 foreach ($_categories as $_category) {
00653                     $_tmpCategory = Mage::getModel('catalog/category')
00654                         ->setStoreId($store['store_id'])
00655                         ->load($_category['entity_id']);
00656                     $this->_synchronize($_tmpCategory, 'insert');
00657                 }
00658             }
00659             $_tmpCategory = null;
00660         } elseif ($category instanceof Mage_Catalog_Model_Category) {
00661             foreach ($category->getStoreIds() as $storeId) {
00662                 $_tmpCategory = Mage::getModel('catalog/category')
00663                     ->setStoreId($storeId)
00664                     ->load($category->getId());
00665                 $_tmpCategory->setStoreId($storeId);
00666                 $this->_synchronize($_tmpCategory);
00667             }
00668             $_tmpCategory = null;
00669         }
00670         return $this;
00671     }


Member Data Documentation

$_inactiveCategoryIds = null [protected]

Definition at line 48 of file Flat.php.

$_isRebuilt = null [protected]

Definition at line 50 of file Flat.php.

$_loaded = false [protected]

Definition at line 39 of file Flat.php.

$_nodes = array() [protected]

Definition at line 41 of file Flat.php.

$_storeId = null [protected]

Definition at line 37 of file Flat.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