No category tree in Magento backend

Perhaps you now the problem shown below. The category tree of the products isn’t rendered correctly. That’s because I don’t set level and children_count during import of categories. This little script corrects this for me: UPDATE catalog_category_entity SET level = (SELECT LENGTH(path)-LENGTH(REPLACE(path,’/’,”)) AS tmpl FROM (SELECT * FROM catalog_category_entity) AS table1 WHERE catalog_category_entity.entity_id = table1.entity_id); UPDATE […]

Debug database queries with parameters

Sometimes you have to query the database directly in your module. In these situations you can use the placeholder feature of the Zend_Db_Statement component. :placeholder will be replaced by the respective value when calling fetch on a Zend_Db_Adapter_Mysqli instance with an array of replacements for example. If you need to debug the query with all parameters in it, […]

Change config during runtime

Sometimes you need to change the Magento configuration during runtime. For example this is the way to temporarily disable the category flat tables <?php Mage::app()->getCacheInstance()->banUse(‘config’); //better switch of config cache Mage::app()->getStore()->setConfig(‘catalog/frontend/flat_catalog_category’, “0”); // … Do wired stuff … Mage::app()->getStore()->setConfig(‘catalog/frontend/flat_catalog_category’, “1”); Attention: You have to reindex categories afterwards!!!

Disable usage of flat tables for categories via code

<?php $oCategoryCollection = Mage::getModel(‘catalog/category’, array(‘disable_flat’ => true))->getCollection(); That’s it! Now the “normal” entity tables are used. That work’s because the field disable_flat decides what kind of collection is loaded in the constructor of Mage_Catalog_Model_Category: <?php class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract { //… /** * Initialize resource mode * * @return void */ protected function _construct() { // […]

Simulate store to set or get store specific values

In earlier Magento versions it was quite difficult to set or read store specific values on entities. In most cases you have to set the store on the whole application instance like that: <?php Mage::app(STOREID)->setStore(STOREID); … $oProduct->setStore(STOREID); … $oCollection->setStore(STOREID); Sometimes it was even necessary to add the store  to entities or collections it-selves too. Now (at least […]

Install community channel

Sometimes it is necessary to install the community channel manually. F.e. in EE. 1.14.0.1 I got the following error massage in the Magento-Connect-Manager shell when I tried to install a package via “Direct package file upload”: CONNECT ERROR: The ‘community’ channel is not installed. Please use the MAGE shell script to install the ‘community’ channel. What […]

Get all Root-Category-IDs of all stores

This snippet gives you all entity_id’s of all Root-IDs of the Magento instance: /** * @return array Root-IDs of all stores */ public function getAllRootIds(){# $aRootIds = array(); $aRootIds[Mage_Catalog_Model_Category::TREE_ROOT_ID] = Mage_Catalog_Model_Category::TREE_ROOT_ID; $aAllStores = Mage::app()->getStores(); foreach ($aAllStores as $iID => $store){ $rootID = $store->getRootCategoryId(); $aRootIds[$rootID]=$rootID; } return array_keys($aRootIds); }  

Error cause by DEFINER in MySQL-dumps

When importing MySQL- dumps of a Magento database sometimes you get an error message like that (although you can connect to the database via shell f.e.): ERROR 1044 (42000) at line 592: Access denied for user ‘sheldon’@’localhost’ to database ‘magento’ In these cases you’ ll recognize that the user, who tries to connect, is wrong […]

Install attribute in entity main table

Sometimes it is useful to add some fields to the attribute main table (f.e.: IDs from external systems like a middleware or a PIM). In the following example you can see how to add ID-fields to the category entity. Therefore I created a setupscript as an instance of Mage_Catalog_Model_Resource_Setup. Setup-model declaration in config.xml: <?xml version=”1.0″?> […]