Mage_Catalog_Model_Resource_Eav_Mysql4_Category Class Reference

Inheritance diagram for Mage_Catalog_Model_Resource_Eav_Mysql4_Category:

Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract Mage_Eav_Model_Entity_Abstract Mage_Core_Model_Resource_Abstract Mage_Eav_Model_Entity_Interface

List of all members.

Public Member Functions

 __construct ()
 setStoreId ($storeId)
 getStoreId ()
 getStoreIds ($category)
 getProductsPosition ($category)
 getChildrenCount ($categoryId)
 move ($categoryId, $newParentId)
 checkId ($id)
 verifyIds (array $ids)
 getChildrenAmount ($category, $isActiveFlag=true)
 refreshProductIndex ($categoryIds=array(), $productIds=array(), $storeIds=array())
 findWhereAttributeIs ($entityIdsFilter, $attribute, $expectedValue)
 getProductCount ($category)
 getParentCategories ($category)
 getChildrenCategories ($category)
 getChildren ($category, $recursive=true)
 getAllChildren ($category)
 isInRootCategoryList ($category)
 isForbiddenToDelete ($categoryId)

Protected Member Functions

 _getTree ()
 _beforeDelete (Varien_Object $object)
 _beforeSave (Varien_Object $object)
 _afterSave (Varien_Object $object)
 _savePath ($object)
 _getMaxPosition ($path)
 _saveCategoryProducts ($category)
 _getIsActiveAttributeId ()
 _saveCountChidren ($object)
 _saveInStores (Varien_Object $object)
 _updateCategoryPath ($category, $path)

Protected Attributes

 $_tree
 $_categoryProductTable
 $_isActiveAttributeId = null
 $_storeId = null


Detailed Description

Definition at line 34 of file Category.php.


Constructor & Destructor Documentation

__construct (  ) 

Class constructor

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 67 of file Category.php.

00068     {
00069         $resource = Mage::getSingleton('core/resource');
00070         $this->setType('catalog_category')
00071             ->setConnection(
00072                 $resource->getConnection('catalog_read'),
00073                 $resource->getConnection('catalog_write')
00074             );
00075         $this->_categoryProductTable = $this->getTable('catalog/category_product');
00076     }


Member Function Documentation

_afterSave ( Varien_Object object  )  [protected]

Process category data after save category object save related products ids and update path value

Parameters:
Varien_Object $object
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Add identifier for new category

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 211 of file Category.php.

00212     {
00213         /**
00214          * Add identifier for new category
00215          */
00216         if (substr($object->getPath(), -1) == '/') {
00217             $object->setPath($object->getPath() . $object->getId());
00218             $this->_savePath($object);
00219         }
00220 
00221         $this->_saveCategoryProducts($object);
00222         return parent::_afterSave($object);
00223     }

_beforeDelete ( Varien_Object object  )  [protected]

Process category data before delete update children count for parent category delete child categories

Parameters:
Varien_Object $object
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Update children count for all parent categories

Recursion use a lot of memmory, that why we run one request for delete children

Add deleted children ids to object This data can be used in after delete event

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 125 of file Category.php.

00126     {
00127         parent::_beforeDelete($object);
00128 
00129         /**
00130          * Update children count for all parent categories
00131          */
00132         $parentIds = $object->getParentIds();
00133         $childDecrease = $object->getChildrenCount() + 1; // +1 is itself
00134         $this->_getWriteAdapter()->update(
00135             $this->getEntityTable(),
00136             array('children_count'=>new Zend_Db_Expr('`children_count`-'.$childDecrease)),
00137             $this->_getWriteAdapter()->quoteInto('entity_id IN(?)', $parentIds)
00138         );
00139 
00140         /**
00141          * Recursion use a lot of memmory, that why we run one request for delete children
00142          */
00143         /*if ($child = $this->_getTree()->getNodeById($object->getId())) {
00144             $children = $child->getChildren();
00145             foreach ($children as $child) {
00146                 $childObject = Mage::getModel('catalog/category')->load($child->getId())->delete();
00147             }
00148         }*/
00149 
00150         $select = $this->_getWriteAdapter()->select()
00151             ->from($this->getEntityTable(), array('entity_id'))
00152             ->where($this->_getWriteAdapter()->quoteInto('`path` LIKE ?', $object->getPath().'/%'));
00153 
00154         $childrenIds = $this->_getWriteAdapter()->fetchCol($select);
00155 
00156         if (!empty($childrenIds)) {
00157             $this->_getWriteAdapter()->delete(
00158                 $this->getEntityTable(),
00159                 $this->_getWriteAdapter()->quoteInto('entity_id IN (?)', $childrenIds)
00160             );
00161         }
00162 
00163         /**
00164          * Add deleted children ids to object
00165          * This data can be used in after delete event
00166          */
00167         $object->setDeletedChildrenIds($childrenIds);
00168         return $this;
00169     }

_beforeSave ( Varien_Object object  )  [protected]

Process category data before saving prepare path and increment children count for parent categories

Parameters:
Varien_Object $object
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 178 of file Category.php.

00179     {
00180         parent::_beforeSave($object);
00181 
00182         if (!$object->getId()) {
00183             $object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
00184             $path  = explode('/', $object->getPath());
00185             $level = count($path);
00186             $object->setLevel($level);
00187             if ($level) {
00188                 $object->setParentId($path[$level - 1]);
00189             }
00190             $object->setPath($object->getPath() . '/');
00191 
00192             $toUpdateChild = explode('/',$object->getPath());
00193 
00194             $this->_getWriteAdapter()->update(
00195                 $this->getEntityTable(),
00196                 array('children_count'=>new Zend_Db_Expr('`children_count`+1')),
00197                 $this->_getWriteAdapter()->quoteInto('entity_id IN(?)', $toUpdateChild)
00198             );
00199 
00200         }
00201         return $this;
00202     }

_getIsActiveAttributeId (  )  [protected]

Get "is_active" attribute identifier

Returns:
int

Definition at line 548 of file Category.php.

00549     {
00550         if (is_null($this->_isActiveAttributeId)) {
00551             $select = $this->_getReadAdapter()->select()
00552                 ->from(array('a'=>$this->getTable('eav/attribute')), array('attribute_id'))
00553                 ->join(array('t'=>$this->getTable('eav/entity_type')), 'a.entity_type_id = t.entity_type_id')
00554                 ->where('entity_type_code = ?', 'catalog_category')
00555                 ->where('attribute_code = ?', 'is_active');
00556 
00557             $this->_isActiveAttributeId = $this->_getReadAdapter()->fetchOne($select);
00558         }
00559         return $this->_isActiveAttributeId;
00560     }

_getMaxPosition ( path  )  [protected]

Definition at line 243 of file Category.php.

00244     {
00245         $select = $this->getReadConnection()->select();
00246         $select->from($this->getTable('catalog/category'), 'MAX(position)');
00247         $select->where('path ?', new Zend_Db_Expr("regexp '{$path}/[0-9]+\$'"));
00248 
00249         $result = 0;
00250         try {
00251             $result = (int) $this->getReadConnection()->fetchOne($select);
00252         } catch (Exception $e) {
00253 
00254         }
00255         return $result;
00256     }

_getTree (  )  [protected]

Retrieve category tree object

Returns:
Varien_Data_Tree_Db

Definition at line 108 of file Category.php.

00109     {
00110         if (!$this->_tree) {
00111             $this->_tree = Mage::getResourceModel('catalog/category_tree')
00112                 ->load();
00113         }
00114         return $this->_tree;
00115     }

_saveCategoryProducts ( category  )  [protected]

Save category products

Parameters:
Mage_Catalog_Model_Category $category
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

new category-product relationships

Example re-save category

old category-product relationships

Find product ids which are presented in both arrays

Use for update just products with changed position

Delete products from category

Add products to category

Update product positions in category

Definition at line 264 of file Category.php.

00265     {
00266         $category->setIsChangedProductList(false);
00267 
00268         /**
00269          * new category-product relationships
00270          */
00271         $products = $category->getPostedProducts();
00272         /**
00273          * Example re-save category
00274          */
00275         if (is_null($products)) {
00276             return $this;
00277         }
00278 
00279         /**
00280          * old category-product relationships
00281          */
00282         $oldProducts = $category->getProductsPosition();
00283 
00284         $insert = array_diff_key($products, $oldProducts);
00285         $delete = array_diff_key($oldProducts, $products);
00286         /**
00287          * Find product ids which are presented in both arrays
00288          */
00289         $update = array_intersect_key($products, $oldProducts);
00290         /**
00291          * Use for update just products with changed position
00292          */
00293         $update = array_diff_assoc($update, $oldProducts);
00294 
00295         $productTable = $this->getTable('catalog/product');
00296         $productUpdateSql = sprintf('UPDATE `%s` AS `e` SET `category_ids`=(SELECT
00297             GROUP_CONCAT(`category_id`) FROM `%s` AS `cp` WHERE `cp`.`product_id`=`e`.`entity_id`)
00298             WHERE `e`.`entity_id` IN(?)', $productTable, $this->_categoryProductTable);
00299 
00300         /**
00301          * Delete products from category
00302          *
00303          */
00304         if (!empty($delete)) {
00305             $deleteIds = array_keys($delete);
00306             $this->_getWriteAdapter()->delete($this->_categoryProductTable,
00307                 $this->_getWriteAdapter()->quoteInto('product_id in(?)', $deleteIds) .
00308                 $this->_getWriteAdapter()->quoteInto(' AND category_id=?', $category->getId())
00309             );
00310 
00311             $sql = $this->_getWriteAdapter()->quoteInto($productUpdateSql, $deleteIds);
00312             $this->_getWriteAdapter()->query($sql);
00313         }
00314 
00315         /**
00316          * Add products to category
00317          *
00318          */
00319         if (!empty($insert)) {
00320             $insertSql = array();
00321             foreach ($insert as $k => $v) {
00322                 $insertSql[] = '('.(int)$category->getId().','.(int)$k.','.(int)$v.')';
00323             }
00324             $sql = sprintf(
00325                 'INSERT INTO `%s` (`category_id`,`product_id`,`position`) VALUES%s',
00326                 $this->_categoryProductTable,
00327                 join(',', $insertSql)
00328             );
00329             $this->_getWriteAdapter()->query($sql);
00330 
00331             $insertIds = array_keys($insert);
00332             $sql = $this->_getWriteAdapter()->quoteInto($productUpdateSql, $insertIds);
00333             $this->_getWriteAdapter()->query($sql);
00334         }
00335 
00336         /**
00337          * Update product positions in category
00338          *
00339          */
00340         if (!empty($update)) {
00341             foreach ($update as $k => $v) {
00342                 $cond   = array(
00343                     $this->_getWriteAdapter()->quoteInto('category_id=?', (int)$category->getId()),
00344                     $this->_getWriteAdapter()->quoteInto('product_id=?', (int)$k)
00345                 );
00346                 $where  = join(' AND ', $cond);
00347                 $bind   = array(
00348                     'position' => (int)$v
00349                 );
00350                 $this->_getWriteAdapter()->update($this->_categoryProductTable, $bind, $where);
00351             }
00352         }
00353 
00354         if (!empty($insert) || !empty($delete)) {
00355             $productIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
00356             Mage::dispatchEvent('catalog_category_change_products', array(
00357                 'category'      => $category,
00358                 'product_ids'   => $productIds
00359             ));
00360         }
00361 
00362         if (!empty($insert) || !empty($update) || !empty($delete)) {
00363             $category->setIsChangedProductList(true);
00364             $categoryIds = explode('/', $category->getPath());
00365             $this->refreshProductIndex($categoryIds);
00366         }
00367 
00368         return $this;
00369     }

_saveCountChidren ( object  )  [protected]

Deprecated since 1.1.7

Parameters:
Varien_Object $object
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Definition at line 737 of file Category.php.

00738     {
00739         $chidren = $object->getChildren();
00740         if (strlen($chidren)>0) {
00741             $chidrenCount = count(explode(',', $chidren));
00742         } else {
00743             $chidrenCount = 0;
00744         }
00745         $this->_getWriteAdapter()->update($this->getEntityTable(),
00746             array('children_count'=>$chidrenCount),
00747             $this->_getWriteAdapter()->quoteInto('entity_id=?', $object->getId())
00748         );
00749 
00750         return $this;
00751     }

_saveInStores ( Varien_Object object  )  [protected]

Deprecated

Parameters:
Varien_Object $object
Returns:
unknown

Definition at line 759 of file Category.php.

00760     {
00761         if (!$object->getMultistoreSaveFlag()) {
00762             $stores = $object->getStoreIds();
00763             foreach ($stores as $storeId) {
00764                 if ($object->getStoreId() != $storeId) {
00765                     $newObject = clone $object;
00766                     $newObject->setStoreId($storeId)
00767                        ->setMultistoreSaveFlag(true)
00768                        ->save();
00769                 }
00770             }
00771         }
00772         return $this;
00773     }

_savePath ( object  )  [protected]

Update path field

Parameters:
Mage_Catalog_Model_Category $object
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Definition at line 231 of file Category.php.

00232     {
00233         if ($object->getId()) {
00234             $this->_getWriteAdapter()->update(
00235                 $this->getEntityTable(),
00236                 array('path'=>$object->getPath()),
00237                 $this->_getWriteAdapter()->quoteInto('entity_id=?', $object->getId())
00238             );
00239         }
00240         return $this;
00241     }

_updateCategoryPath ( category,
path 
) [protected]

Deprecated

Definition at line 778 of file Category.php.

00779     {
00780         return $this;
00781         if ($category->getNotUpdateDepends()) {
00782             return $this;
00783         }
00784         foreach ($path as $pathItem) {
00785             if ($pathItem->getId()>1 && $category->getId() != $pathItem->getId()) {
00786                 $category = Mage::getModel('catalog/category')
00787                     ->load($pathItem->getId())
00788                     ->save();
00789             }
00790         }
00791         return $this;
00792     }

checkId ( id  ) 

Check if category id exist

Parameters:
int $id
Returns:
bool

Definition at line 485 of file Category.php.

00486     {
00487         $select = $this->_getReadAdapter()->select()
00488             ->from($this->getEntityTable(), 'entity_id')
00489             ->where('entity_id=?', $id);
00490         return $this->_getReadAdapter()->fetchOne($select);
00491     }

findWhereAttributeIs ( entityIdsFilter,
attribute,
expectedValue 
)

Definition at line 690 of file Category.php.

00691     {
00692         $select = $this->_getReadAdapter()->select()
00693             ->from($attribute->getBackend()->getTable(), array('entity_id'))
00694             ->where('attribute_id = ?', $attribute->getId())
00695             ->where('value = ?', $expectedValue)
00696             ->where('entity_id in (?)', $entityIdsFilter);
00697 
00698         return $this->_getReadAdapter()->fetchCol($select);
00699     }

getAllChildren ( category  ) 

Return all children ids of category (with category id)

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 907 of file Category.php.

00908     {
00909         $children = $this->getChildren($category);
00910         $myId = array($category->getId());
00911         $children = array_merge($myId, $children);
00912 
00913         return $children;
00914 
00915 //        $this->_getTree()->load();
00916 //        $children = $this->_getTree()->getChildren($category->getId());
00917 //
00918 //        $myId = array($category->getId());
00919 //        if (is_array($children)) {
00920 //            $children = array_merge($myId, $children);
00921 //        } else {
00922 //            $children = $myId;
00923 //        }
00924 //
00925 //        return $children;
00926     }

getChildren ( category,
recursive = true 
)

Return children ids of category

Parameters:
Mage_Catalog_Model_Category $category
boolean $recursive
Returns:
array

Definition at line 869 of file Category.php.

00870     {
00871         $attributeId = $this->_getIsActiveAttributeId();
00872         $select = $this->_getReadAdapter()->select()
00873             ->from(array('m' => $this->getEntityTable()), 'entity_id')
00874             ->joinLeft(
00875                 array('d' => $this->getEntityTable() . '_int'),
00876                 "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id",
00877                 array()
00878             )
00879             ->joinLeft(
00880                 array('c' => $this->getEntityTable() . '_int'),
00881                 "c.attribute_id = '{$attributeId}' AND c.store_id = '{$category->getStoreId()}' AND c.entity_id = m.entity_id",
00882                 array()
00883             )
00884             ->where('(IFNULL(c.value, d.value) = ?)', '1')
00885             ->where('path LIKE ?', "{$category->getPath()}/%");
00886         if (!$recursive) {
00887             $select->where('level <= ?', $category->getLevel() + 1);
00888         }
00889         $_categories = $this->_getReadAdapter()->fetchAll($select);
00890         $categoriesIds = array();
00891         foreach ($_categories as $_category) {
00892             $categoriesIds[] = $_category['entity_id'];
00893         }
00894 
00895         return $categoriesIds;
00896 
00897 //        $this->_getTree()->load();
00898 //        return $this->_getTree()->getChildren($category->getId(), false);
00899     }

getChildrenAmount ( category,
isActiveFlag = true 
)

Get count of active/not active children categories

Parameters:
Mage_Catalog_Model_Category $category
bool $isActiveFlag
Returns:
int

Definition at line 519 of file Category.php.

00520     {
00521         $storeId = Mage::app()->getStore()->getId();
00522         $attributeId = $this->_getIsActiveAttributeId();
00523         $table = Mage::getSingleton('core/resource')->getTableName('catalog/category') . '_int';
00524 
00525         $select = $this->_getReadAdapter()->select()
00526             ->from(array('m'=>$this->getEntityTable()), array('COUNT(m.entity_id)'))
00527             ->joinLeft(
00528                 array('d'=>$table),
00529                 "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id",
00530                 array()
00531             )
00532             ->joinLeft(
00533                 array('c'=>$table),
00534                 "c.attribute_id = '{$attributeId}' AND c.store_id = '{$storeId}' AND c.entity_id = m.entity_id",
00535                 array()
00536             )
00537             ->where('m.path like ?', $category->getPath() . '/%')
00538             ->where('(IFNULL(c.value, d.value) = ?)', $isActiveFlag);
00539 
00540         return $this->_getReadAdapter()->fetchOne($select);
00541     }

getChildrenCategories ( category  ) 

Enter description here...

Parameters:
Mage_Catalog_Model_Category $category
Returns:
unknown

Definition at line 846 of file Category.php.

00847     {
00848         $collection = $category->getCollection();
00849         /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
00850         $collection->addAttributeToSelect('url_key')
00851             ->addAttributeToSelect('name')
00852             ->addAttributeToSelect('all_children')
00853             ->addAttributeToSelect('is_anchor')
00854             ->addAttributeToFilter('is_active', 1)
00855             ->addIdFilter($category->getChildren())
00856             ->setOrder('position', 'ASC')
00857             ->joinUrlRewrite()
00858             ->load();
00859         return $collection;
00860     }

getChildrenCount ( categoryId  ) 

Get chlden categories count

Parameters:
int $categoryId
Returns:
int

Definition at line 429 of file Category.php.

00430     {
00431         $select = $this->_getReadAdapter()->select()
00432             ->from($this->getEntityTable(), 'children_count')
00433             ->where('entity_id=?', $categoryId);
00434 
00435         $child = $this->_getReadAdapter()->fetchOne($select);
00436 
00437         return $child;
00438     }

getParentCategories ( category  ) 

Return parent categories of category

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 826 of file Category.php.

00827     {
00828         $pathIds = array_reverse(explode(',', $category->getPathInStore()));
00829         $categories = Mage::getResourceModel('catalog/category_collection')
00830             ->setStore(Mage::app()->getStore())
00831             ->addAttributeToSelect('name')
00832             ->addAttributeToSelect('url_key')
00833             ->addFieldToFilter('entity_id', array('in'=>$pathIds))
00834             ->addFieldToFilter('is_active', 1)
00835             ->load()
00836             ->getItems();
00837         return $categories;
00838     }

getProductCount ( category  ) 

Get products count in category

Parameters:
unknown_type $category
Returns:
unknown

Definition at line 707 of file Category.php.

00708     {
00709         $productTable =Mage::getSingleton('core/resource')->getTableName('catalog/category_product');
00710 
00711         $select =  $this->getReadConnection()->select();
00712         $select->from(
00713             array('main_table'=>$productTable),
00714             array(new Zend_Db_Expr('COUNT(main_table.product_id)'))
00715         )
00716         ->where('main_table.category_id = ?', $category->getId())
00717         ->group('main_table.category_id');
00718 
00719         $counts =$this->getReadConnection()->fetchOne($select);
00720 
00721         return intval($counts);
00722     }

getProductsPosition ( category  ) 

Get positions of associated to category products

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 414 of file Category.php.

00415     {
00416         $select = $this->_getWriteAdapter()->select()
00417             ->from($this->_categoryProductTable, array('product_id', 'position'))
00418             ->where('category_id=?', $category->getId());
00419         $positions = $this->_getWriteAdapter()->fetchPairs($select);
00420         return $positions;
00421     }

getStoreId (  ) 

Return store id

Returns:
integer

Definition at line 95 of file Category.php.

00096     {
00097         if (is_null($this->_storeId)) {
00098             return Mage::app()->getStore()->getId();
00099         }
00100         return $this->_storeId;
00101     }

getStoreIds ( category  ) 

Get store identifiers where category is presented

Parameters:
Mage_Catalog_Model_Category $category
Returns:
array

Definition at line 377 of file Category.php.

00378     {
00379         if (!$category->getId()) {
00380             return array();
00381         }
00382 
00383         $nodePath = $this->_getTree()
00384             ->getNodeById($category->getId())
00385                 ->getPath();
00386 
00387         $nodes = array();
00388         foreach ($nodePath as $node) {
00389             $nodes[] = $node->getId();
00390         }
00391 
00392         $stores = array();
00393         $storeCollection = Mage::getModel('core/store')->getCollection()->loadByCategoryIds($nodes);
00394         foreach ($storeCollection as $store) {
00395             $stores[$store->getId()] = $store->getId();
00396         }
00397 
00398         $entityStoreId = $category->getStoreId();
00399         if (!in_array($entityStoreId, $stores)) {
00400             array_unshift($stores, $entityStoreId);
00401         }
00402         if (!in_array(0, $stores)) {
00403             array_unshift($stores, 0);
00404         }
00405         return $stores;
00406     }

isForbiddenToDelete ( categoryId  ) 

Check category is forbidden to delete.

If category is root and assigned to store group return false

Parameters:
integer $categoryId
Returns:
boolean

Definition at line 962 of file Category.php.

00963     {
00964         $select = $this->_getReadAdapter()->select()
00965             ->from($this->getTable('core/store_group'), array('group_id'))
00966             ->where('root_category_id = ?', $categoryId);
00967         if ($this->_getReadAdapter()->fetchOne($select)) {
00968             return true;
00969         }
00970         return false;
00971     }

isInRootCategoryList ( category  ) 

Check is category in list of store categories

Parameters:
Mage_Catalog_Model_Category $category
Returns:
boolean

Definition at line 934 of file Category.php.

00935     {
00936         $innerSelect = $this->_getReadAdapter()->select()
00937             ->from($this->getEntityTable(), new Zend_Db_Expr("CONCAT(path, '/%')"))
00938             ->where('entity_id = ?', Mage::app()->getStore()->getRootCategoryId());
00939         $select = $this->_getReadAdapter()->select()
00940             ->from($this->getEntityTable(), 'entity_id')
00941             ->where('entity_id = ?', $category->getId())
00942             ->where(new Zend_Db_Expr("path LIKE ({$innerSelect->__toString()})"));
00943         return (bool) $this->_getReadAdapter()->fetchOne($select);
00944 
00945 //        $tree = $this->_getTree();
00946 //        $tree->load();
00947 //        $children = $tree->getChildren(Mage::app()->getStore()->getRootCategoryId(), true);
00948 //        if (!in_array($category->getId(), $children)) {
00949 //            return false;
00950 //        }
00951 //        return true;
00952     }

move ( categoryId,
newParentId 
)

Move category to another parent

Parameters:
int $categoryId
int $newParentId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Definition at line 447 of file Category.php.

00448     {
00449         $category  = Mage::getModel('catalog/category')->load($categoryId);
00450         $oldParent = $category->getParentCategory();
00451         $newParent = Mage::getModel('catalog/category')->load($newParentId);
00452 
00453         $childrenCount = $this->getChildrenCount($category->getId()) + 1;
00454 
00455         // update children count of new parents
00456         $parentIds = explode('/', $newParent->getPath());
00457         $this->_getWriteAdapter()->update(
00458             $this->getEntityTable(),
00459             array('children_count' => new Zend_Db_Expr("`children_count` + {$childrenCount}")),
00460             $this->_getWriteAdapter()->quoteInto('entity_id IN (?)', $parentIds)
00461         );
00462 
00463         // update children count of old parents
00464           $parentIds = explode('/', $oldParent->getPath());
00465           $this->_getWriteAdapter()->update(
00466             $this->getEntityTable(),
00467             array('children_count' => new Zend_Db_Expr("`children_count` - {$childrenCount}")),
00468             $this->_getWriteAdapter()->quoteInto('entity_id IN (?)', $parentIds)
00469         );
00470 
00471         // update parent id
00472         $this->_getWriteAdapter()->query("UPDATE
00473             {$this->getEntityTable()} SET parent_id = {$newParent->getId()}
00474             WHERE entity_id = {$categoryId}");
00475 
00476         return $this;
00477     }

refreshProductIndex ( categoryIds = array(),
productIds = array(),
storeIds = array() 
)

Rebuild associated products index

Parameters:
array $categoryIds
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Prepare visibility and status attributes information

Select categories data

Get information about stores root categories

Definition at line 568 of file Category.php.

00569     {
00570         /**
00571          * Prepare visibility and status attributes information
00572          */
00573         $statusAttribute        = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'status');
00574         $visibilityAttribute    = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'visibility');
00575         $statusAttributeId      = $statusAttribute->getId();
00576         $visibilityAttributeId  = $visibilityAttribute->getId();
00577         $statusTable            = $statusAttribute->getBackend()->getTable();
00578         $visibilityTable        = $visibilityAttribute->getBackend()->getTable();
00579 
00580         /**
00581          * Select categories data
00582          */
00583         $select = $this->_getReadAdapter()->select()
00584             ->from($this->getTable('catalog/category'))
00585             ->order('level')
00586             ->order('path');
00587 
00588         if (is_array($categoryIds) && !empty($categoryIds)) {
00589             $select->where('entity_id IN (?)', $categoryIds);
00590         } elseif (is_numeric($categoryIds)) {
00591             $select->where('entity_id=?', $categoryIds);
00592         }
00593 
00594         $categories = $this->_getWriteAdapter()->fetchAll($select);
00595 
00596         $storesCondition = '';
00597         if (!empty($storeIds)) {
00598             $storesCondition = $this->_getWriteAdapter()->quoteInto(
00599                 ' AND s.store_id IN (?)', $storeIds
00600             );
00601         }
00602 
00603         /**
00604          * Get information about stores root categories
00605          */
00606         $stores = $this->_getWriteAdapter()->fetchAll("
00607             SELECT
00608                 s.store_id, s.website_id, c.path AS root_path
00609             FROM
00610                 {$this->getTable('core/store')} AS s,
00611                 {$this->getTable('core/store_group')} AS sg,
00612                 {$this->getTable('catalog/category')} AS c
00613             WHERE
00614                 sg.group_id=s.group_id
00615                 AND c.entity_id=sg.root_category_id
00616                 {$storesCondition}
00617         ");
00618 
00619         $indexTable = $this->getTable('catalog/category_product_index');
00620 
00621         foreach ($stores as $storeData) {
00622             $storeId    = $storeData['store_id'];
00623             $websiteId  = $storeData['website_id'];
00624             $rootPath   = $storeData['root_path'];
00625 
00626             $productCondition = '';
00627             if (!empty($productIds)) {
00628                 $productCondition = $this->_getWriteAdapter()->quoteInto(
00629                     ' AND product_id IN (?)', $productIds
00630                 );
00631             }
00632             $insProductCondition = str_replace('product_id', 'cp.product_id', $productCondition);
00633 
00634             foreach ($categories as $category) {
00635                 $categoryId = $category['entity_id'];
00636                 $path       = $category['path'];
00637 
00638                 $this->_getWriteAdapter()->delete(
00639                     $indexTable,
00640                     'category_id='.$categoryId. ' AND store_id='.$storeId.$productCondition
00641                 );
00642 
00643                 if (strpos($path.'/', $rootPath.'/') === false) {
00644                     continue;
00645                 }
00646 
00647                 $query = "INSERT INTO {$indexTable}
00648                     (`category_id`, `product_id`, `position`, `is_parent`, `store_id`, `visibility`)
00649                 SELECT
00650                     {$categoryId},
00651                     cp.product_id,
00652                     cp.position,
00653                     MAX({$categoryId}=cp.category_id) as is_parent,
00654                     {$storeId},
00655                     IFNULL(t_v.value, t_v_default.value)
00656                 FROM
00657                     {$this->getTable('catalog/category_product')} AS cp
00658                 INNER JOIN {$this->getTable('catalog/product_website')} AS pw
00659                     ON pw.product_id=cp.product_id AND pw.website_id={$websiteId}
00660                 INNER JOIN {$visibilityTable} AS `t_v_default`
00661                     ON (t_v_default.entity_id = cp.product_id)
00662                         AND (t_v_default.attribute_id='{$visibilityAttributeId}')
00663                         AND t_v_default.store_id=0
00664                 LEFT JOIN {$visibilityTable} AS `t_v`
00665                     ON (t_v.entity_id = cp.product_id)
00666                         AND (t_v.attribute_id='{$visibilityAttributeId}')
00667                         AND (t_v.store_id='{$storeId}')
00668                 INNER JOIN {$statusTable} AS `t_s_default`
00669                     ON (t_s_default.entity_id = cp.product_id)
00670                         AND (t_s_default.attribute_id='{$statusAttributeId}')
00671                         AND t_s_default.store_id=0
00672                 LEFT JOIN {$statusTable} AS `t_s`
00673                     ON (t_s.entity_id = cp.product_id)
00674                         AND (t_s.attribute_id='{$statusAttributeId}')
00675                         AND (t_s.store_id='{$storeId}')
00676                 WHERE category_id IN(
00677                     SELECT entity_id FROM {$this->getTable('catalog/category')}
00678                     WHERE entity_id = {$category['entity_id']} OR path LIKE '{$path}/%')
00679                     AND (IFNULL(t_s.value, t_s_default.value)=".Mage_Catalog_Model_Product_Status::STATUS_ENABLED.")
00680                     {$insProductCondition}
00681                 GROUP BY product_id
00682                 ORDER BY is_parent desc";
00683 
00684                 $this->_getWriteAdapter()->query($query);
00685             }
00686         }
00687         return $this;
00688     }

setStoreId ( storeId  ) 

Set store Id

Parameters:
integer $storeId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Category

Definition at line 84 of file Category.php.

00085     {
00086         $this->_storeId = $storeId;
00087         return $this;
00088     }

verifyIds ( array ids  ) 

Check array of category identifiers

Parameters:
array $ids
Returns:
array

Definition at line 499 of file Category.php.

00500     {
00501         $validIds = array();
00502         $select = $this->_getWriteAdapter()->select()
00503             ->from($this->getEntityTable(), 'entity_id')
00504             ->where('entity_id IN(?)', $ids);
00505         $query = $this->_getWriteAdapter()->query($select);
00506         while ($row = $query->fetch()) {
00507             $validIds[] = $row['entity_id'];
00508         }
00509         return $validIds;
00510     }


Member Data Documentation

$_categoryProductTable [protected]

Definition at line 48 of file Category.php.

$_isActiveAttributeId = null [protected]

Definition at line 55 of file Category.php.

$_storeId = null [protected]

Definition at line 62 of file Category.php.

$_tree [protected]

Definition at line 41 of file Category.php.


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

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