好的,使用安装脚本似乎有一个奇怪的惯例可以使用以下内容:

$installer = $this;

我真的不明白这一点,因为它是完全多余的。

为什么不只是使用 $this-> 在整个脚本中?

关于为什么存在该惯例的任何想法?

有帮助吗?

解决方案

答案要简单得多。在2007年(我相信直到2009年phpstorm开始摇摆时),没有一个IDE允许IDE提供直列PHPDOC $this. 。但是核心开发人员希望在IDE中进行自动完成。这就是为什么他们使用这2行:

$installer = $this;
/* @var $installer <appropriate class> */

一些模块具有自己的设置类,应该在Inline PHPDOC中使用。但是,由于设置/升级脚本总是通过“复制/粘贴一些现有的脚本并更改”创建 Mage_Eav_Model_Entity_Setup) 但 Mage_Catalog_Model_Resource_Setup 在升级脚本中用于Inline PHPDOC。

其他提示

我拥有的最古老的版本是1.0。即使这样 $installer = $this; 存在。即使在命名的文件中 upgrade-0.x.y-0.z.t 这条线存在。

我认为,当他们开始时(我的意思是0.1版或类似版本),他们有类似的东西 $installer = new Something() 他们决定改变逻辑。
我认为这是因为 <class> 标记在 config.xml 某些模块(例如mage_catalog)。 1.6之前的版本:

<setup>
    <module>Mage_Catalog</module>
    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup> 

或版本1.6+:

<setup>
    <module>Mage_Catalog</module>
    <class>Mage_Catalog_Model_Resource_Setup</class>
</setup>

我通常使用 $this 代替 $installer 而且我没有任何问题(如果以任何方式构成)。

自2007年最早的公共Beta以来,这是一项未知的典型,可能是可疑的逻辑(可能是可疑的)(预览B1 0.6.12383;要求登录)。

它被用作约定,以确保执行设置代码的类在设置脚本中始终存在。例如,两者都 Enterprise_GiftWrappingEnterprise_Rma 模块有自己的设置类,每个别名 $installerMage_Catalog_Model_Resource_Setup 将属性添加到产品实体时,例如:

app/code/core/enterprise/giftwrapping/sql/enterprise_giftwrapping_setup/install-1.11.0.0.0.php

<?php

$installer = $this;
/* @var $installer Enterprise_GiftWrapping_Model_Resource_Setup */
//... miscellaneous Enterprise_GiftWrapping setup logic

$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
//... miscellaneous product entity attribute manipulation

我喜欢使用的一件事 $installer 我真的想补充一点,它可以轻松地用其他东西替换它或在类范围之外运行它。


1.更换它:

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */
// Do basic stuff

$installer = Mage::getSingleton('eav/entity_setup', 'eav_setup');
/* @var $installer Mage_Eav_Model_Entity_Setup */
/// Do stuff with attributes


2.外部范围:

$ php -a
php > require_once 'app/Mage.php';
php > Mage::init();
php > require 'app/code/local/Vendor/Module/data/vendormodule_setup/data-upgrade-0.1.1-0.1.2.php';
Upgrade worked!
Debug data dump: array(4) {
  'id' =>
  int(123)
  'foo' =>
  string(3) "bar"
}

当然是 data-upgrade-0.1.1-0.1.2.php 有:

//$installer = $this;
$installer = Mage::getResourceSingleton('core/setup', 'vendormodule_setup');
/* @var $installer Mage_Core_Model_Resource_Setup */

// Do lots of stuff ...

echo "Upgrade worked!\n";
echo "Debug data dump: ";
var_dump($debug);

因此,这可以防止 Fatal error: Using $this when not in object context

我想是从早期开始的(

我们一直这样做

你知道 ;-)

许可以下: CC-BY-SA归因
scroll top