代码之家  ›  专栏  ›  技术社区  ›  Rakward

设置刚创建的网站的配置值?

  •  4
  • Rakward  · 技术社区  · 15 年前

    我正在按程序创建网站/用户等。。。

    问题是:当创建一个网站时,我不能立即设置配置值。

    代码:

    <?php
    /* Website information */
    $website_data = array(
                'name' => 'Company name',
                'code' => 'website_company_1',
                'sort_order' => '1',
                );
    
    /* Save website */
    $website = Mage::getModel('core/website'); 
    $website->setData($website_data);
    $website->save();
    
    /* Get website code */
    $web_code = $website->getCode();
    
    /* R-int stores */
    Mage::app()->reinitStores();
    
    /* Config data array example */
    $data = array('name' => 'Company 1', 'phone' => '056 22 33 61')
    
    /* Set config values in array */
    $groups = array();
     foreach($data as $key => $value){
     $groups['store_information']['fields'][$key]['value'] = $value;
     }
    
    
    /* Save config values */
    Mage::getModel('adminhtml/config_data')
          ->setSection('general')
          ->setWebsite($web_code)
          ->setStore(NULL)
          ->setGroups($groups)
          ->save();
    
    
    /* Re-init again */
    Mage::app()->reinitStores();
    

    但是,由于某些原因,这不起作用,但是如果我先创建一个网站(使用相同的代码),然后再执行这个config save函数,就可以了。好像它需要先加载一个新的页面,然后才能设置/更新配置值。我以为重新初始化可以解决这个问题,但它没有。。。

    2 回复  |  直到 15 年前
        1
  •  5
  •   Fooman    15 年前

    你没提到你在用哪个Magento版本。我已经在1.4.1.1上测试了下面的代码,并做了一些更改,因此它是一个运行示例。

    Mage::app()->reinitStores();
    

    Mage::app()->getConfig()->reinit();
    

    完整示例:

    <?php
    
    require_once 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
    Mage::app();
    
    /* Website information */
    $website_data = array(
        'name' => 'Website Name',
        'code' => 'website_company',
        'sort_order' => '2',
        'is_active' => 1,
    );
    
    /* Save website */
    $website = Mage::getModel('core/website');
    $website->setData($website_data);
    $website->save()->load();
    
    /* Save store */
    $storeGroup = Mage::getModel('core/store_group');
    $storeGroup->setData(
            array(
                'root_category_id' => '3',
                'website_id' => $website->getId(),
                'name' => 'Store',
            )
    );
    $storeGroup->save()->load();
    
    $store = Mage::getModel('core/store');
    $store->setData(
            array(
                'website_id' => $website->getId(),
                'name' => $storeGroup->getName(),
                'code' => 'store_' . $website->load()->getId(),
                'group_id' => $storeGroup->getGroupId(),
                'is_active' => 1,
            )
    );
    $store->save()->load();
    
    /* Re-init */
    Mage::app()->getConfig()->reinit();
    
    /* Config data array example */
    $data = array('name' => 'Company 1', 'phone' => '056 22 33 61');
    
    /* Set config values in array */
    $groups = array();
    foreach ($data as $key => $value) {
        $groups['store_information']['fields'][$key]['value'] = $value;
    }
    
    /* Save config values */
    $data = Mage::getModel('adminhtml/config_data')
                    ->setSection('general')
                    ->setWebsite($website->getCode())
                    ->setGroups($groups)
                    ->save();
    
        2
  •  7
  •   benmarks    15 年前

    为此,应该使用安装/升级脚本(这些是模块内部的脚本)

    Mage_Core_Model_Resource_Setup 或者让你的设置类从那里扩展。

    看到了吗 setConfigData() 图像\u核心\u模型\u资源\u设置:: deleteConfigData()

    图像\u核心\u模型\u资源\u设置:: addConfigField() 也可以使用,但从我所知道的核心没有实现。

    <?xml version="1.0" ?>
    <!-- module config.xml -->
    <config>
        <modules>
            <Your_Module>
                <version>1.0</version>
                <!-- upgrade script #s evaluated with version_compare(), FYI -->
            </Your_Module>
        </modules>
        <global>
            <resources>
                <unique_node>
                    <setup>
                        <!-- match node under <modules> -->
                        <module>Your_Module</module>
                        <class>Mage_Core_Model_Resource_Setup</class>
                    </setup>
                </unique_node>
            </resources>
        </global>
    </config>
    

    <?php
    
    $installer = $this;
    /* @var $installer Mage_Core_Model_Resource_Setup */ //or whatever you configured
    
    $installer->startSetup();
    
    $installer->setConfigData($path, $value, $scope='default', $scopeId=0) //inherit is not implemented
    
    $installer->endSetup();