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

发布于 2024-09-16 21:54:39 字数 1180 浏览 5 评论 0原文

我正在以编程方式创建网站/用户等...

问题如下:创建网站时,我无法立即设置配置值。

代码:

<?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();

但是,由于某种原因,这不起作用,但如果我先创建一个网站(使用相同的代码),然后执行此配置保存功能,它就可以正常工作。好像它需要先加载新页面才能设置/更新配置值。我以为重新初始化可以解决这个问题,但事实并非如此……

有什么想法吗?

I'm programmtically creating websites/users etc ...

Here's the problem: When creating a website, I can't immediatly set the config values afterwards.

Code:

<?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();

However, this doesn't work for some reason, but if I create a website first(with the same code), then execute this config-save function afterwards, it works fine. As if it needs a new page load first before it can set/update config values. I thought the re-init would solve this but it doesn't ...

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2024-09-23 21:54:39

为此,您应该使用安装/升级脚本(这些是模块的 sql 文件夹内的脚本)。您甚至可能希望创建一个特定于设置的模块来运行这些模块。

只需在模块的全局/资源节点中声明一个设置资源,然后创建完成此操作所需的文件。使用 Mage_Core_Model_Resource_Setup 或者让您的设置类从那里扩展。

请参阅 Mage_Core_Model_Resource_Setup::setConfigData()Mage_Core_Model_Resource_Setup::deleteConfigData()

Mage_Core_Model_Resource_Setup::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();

You should use install/upgrade scripts for this purpose (these are the scripts inside modules' sql folders). You may even wish to create a setup-specific module with/in which to run these.

Simply declare a setup resource in your module's global/resources node and then create the file(s) you need to accomplish this. Use Mage_Core_Model_Resource_Setup or have your setup class extend from there.

See Mage_Core_Model_Resource_Setup::setConfigData() and Mage_Core_Model_Resource_Setup::deleteConfigData().

Mage_Core_Model_Resource_Setup::addConfigField() could also be used but is not implemented in the core from what I can tell.

<?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>

Then in your install/upgrade scripts do this:

<?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();
南冥有猫 2024-09-23 21:54:39

您没有提到您使用的是哪个 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();

You didn't mention which Magento version you are on. I have tested the below on 1.4.1.1 and made some changes so it is a running example.

The main difference is the change from

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

to

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

which re-loads the config while also reloading the cache.

Complete Example:

<?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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文