1

While creating the category via installer, category not created but the table is created,

below i added my installer script,

any wrong in this script?

$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$installer->getTable('membership/members')};
CREATE TABLE `{$installer->getTable('membership/members')}` (
      `memberid` int(11) NOT NULL auto_increment,
      `name` text,
      `email` text,
      `cateid` text,
      `date` datetime default NULL,
      `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
      PRIMARY KEY (`memberid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->run("
DROP TABLE IF EXISTS {$installer->getTable('membership/types')};
CREATE TABLE `{$installer->getTable('membership/types')}` (
      `mtypeid` int(11) NOT NULL auto_increment,
      `typename` text,
      `duration` text,
      `description` text,
      `price` text,
      PRIMARY KEY (`mtypeid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");

$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val)
{
    $_storeId = Mage::app()->getStore($_eachStoreId)->getId();
    $root_catid = Mage::app()->getStore($_storeId)->getRootCategoryId();

    $parentCategory = Mage::getModel('catalog/category')->load($root_catid);
    $childCategory = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('is_active', true)
        ->addIdFilter($parentCategory->getChildren())
        ->addAttributeToFilter('name', 'categoryname')
        ->getFirstItem()    // Assuming your category names are unique ??
    ;

    if ($childCategory->getId() == null) {
        $category = new Mage_Catalog_Model_Category();
        $category->setName('categoryname');
        $category->setUrlKey('att-membership');
        $category->setIsActive(1);
        $category->setDisplayMode('PRODUCTS');
        $category->setIsAnchor(0);

        $parentCategory = Mage::getModel('catalog/category')->load($root_catid);
        $category->setPath($parentCategory->getPath());              

        $category->save();
        unset($category);
    } 
}

$installer->endSetup();
  • 1
    Try moving the code for creating the category to the data folder of your module in the file data-install-1.0.0.php. Replace (1.0.0 with your version number) – Marius Feb 12 '14 at 11:43
  • Please write what you did as an answer and accept it. Don't leave zombie questions. – Marius Feb 12 '14 at 12:22
1

I have split the mysql4-install-1.0.0.php into 2 files ,

modulename\sql\membership_setup\mysql4-install-1.0.0.php, shows below

$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$installer->getTable('membership/members')};
CREATE TABLE `{$installer->getTable('membership/members')}` (
      `memberid` int(11) NOT NULL auto_increment,
      `name` text,
      `email` text,
      `cateid` text,
      `date` datetime default NULL,
      `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
      PRIMARY KEY (`memberid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->run("
DROP TABLE IF EXISTS {$installer->getTable('membership/types')};
CREATE TABLE `{$installer->getTable('membership/types')}` (
      `mtypeid` int(11) NOT NULL auto_increment,
      `typename` text,
      `duration` text,
      `description` text,
      `price` text,
      PRIMARY KEY (`mtypeid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");


$installer->endSetup();

data file "modulename\data\membership_setup\data-install-1.0.0.php",

$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val)
{
    $_storeId = Mage::app()->getStore($_eachStoreId)->getId();
    $root_catid = Mage::app()->getStore($_storeId)->getRootCategoryId();

    $parentCategory = Mage::getModel('catalog/category')->load($root_catid);
    $childCategory = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('is_active', true)
        ->addIdFilter($parentCategory->getChildren())
        ->addAttributeToFilter('name', 'AttVirMembership')
        ->getFirstItem()    // Assuming your category names are unique ??
    ;

    if ($childCategory->getId() == null) {
        $category = new Mage_Catalog_Model_Category();
        $category->setName('AttVirMembership');
        $category->setUrlKey('att-vir-membership');
        $category->setIsActive(1);
        $category->setDisplayMode('PRODUCTS');
        $category->setIsAnchor(0);

        $parentCategory = Mage::getModel('catalog/category')->load($root_catid);
        $category->setPath($parentCategory->getPath());              

        $category->save();
        unset($category);
    } 
}

ie. mysql query in the - mysql file and php script in the data file,

then its worked well.

thanks Marius,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.