2012-04-13 5 views
1

私は、以下のリンクに記載されているコードを使用しています属性:属性セットを作成し、プログラム

http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/programmatically_adding_attributes_and_attribute_sets

すべてがポイントまで動作します:

// Just add a default group. 
    else 
    { 
     $this->logInfo("Creating default group [{$this->groupName}] for set."); 

     $modelGroup = Mage::getModel('eav/entity_attribute_group'); 
     $modelGroup->setAttributeGroupName($this->groupName); 
     $modelGroup->setAttributeSetId($id); 

     // This is optional, and just a sorting index in the case of 
     // multiple groups. 
     // $modelGroup->setSortOrder(1); 

     $model->setGroups(array($modelGroup)); 
    } 

オブジェクト参照が希望私はわかりませんよから設定する必要があります - 私はこれを独立したファイルとして自動化できるようにしようとしています - これを実行してこのファイルを実行しています

require_once 'app/Mage.php'; 
Mage::app(); 

この中にすべてのヘルプは大モジュールがインストール(またはアップグレード)されたときにMagentoのは、それをロードするようにするには、インストーラとしてこれを実装する必要があり

+1

どのオブジェクトが参照されていますか? –

+0

logInfoの代わりに、Mage :: log()を使用する必要があります。 –

+0

ありがとうございましたVern - これを行います - 私は何かを返さない$ this-> groupNameを参照しています - それを別のスクリプトとして使用すると、グループ名を取得して設定するにはどうすればこのオブジェクトをインスタンス化できますか? –

答えて

12

あなたはこれがあなたのXMLにあるディレクトリにインストーラコードを配置することができますので、

<resources> 
    <namespace_module_setup><!-- becomes folder under sql dir --> 
     <setup> 
      <module>Namespace_Module</module> 
      <class>Mage_Eav_Model_Entity_Setup</class> 
     </setup> 
    </namespace_module_setup> 
</resources> 

のように見えるあなたのモジュールのconfig.xmlにブロックを持っている必要があります。インストーラファイルに記載されているバージョンがモジュールの<version>1.2.0</version>と一致することを確認する必要があります。それ以外の場合、Magentoはインストーラを実行できません。属性セットを追加するには、以下のデータを使用することができます。私はそれを使用しませんでしたが、entityTypeIdは、それが顧客、出荷、カテゴリ、製品エンティティのどれであるかをそれぞれ1、2、3、4と定義します。

/** 
    * Add Attribute Set 
    * 
    * @param mixed $entityTypeId 
    * @param string $name 
    * @param int $sortOrder 
    * @return Mage_Eav_Model_Entity_Setup 
    */ 
    public function addAttributeSet($entityTypeId, $name, $sortOrder = null) 
    { 
     $data = array(
      'entity_type_id'  => $this->getEntityTypeId($entityTypeId), 
      'attribute_set_name' => $name, 
      'sort_order'   => $this->getAttributeSetSortOrder($entityTypeId, $sortOrder), 
     ); 

     $setId = $this->getAttributeSet($entityTypeId, $name, 'attribute_set_id'); 
     if ($setId) { 
      $this->updateAttributeSet($entityTypeId, $setId, $data); 
     } else { 
      $this->_conn->insert($this->getTable('eav/attribute_set'), $data); 

      $this->addAttributeGroup($entityTypeId, $name, $this->_generalGroupName); 
     } 

     return $this; 
    } 

これはセットに属性を追加するためのコードであり、単に属性を変更するには、上記データ

//app/code/local/Namespace/Module/sql/Namespace_Module_setup/mysql4-install-1.0.0.php 
    $installer = $this; 
    /* @var $installer Mage_Eav_Model_Entity_Setup */ 

    $installer->startSetup(); 

     $data= array (
      'attribute_set' => 'Default', 
      'group' => 'General', 
      'label' => 'Some Label', 
      'visible'  => true, 
      'type'  => 'varchar', // multiselect uses comma-sep storage 
      'input' => 'text', 
      'system' => true, 
      'required' => false, 
      'user_defined' => 1, //defaults to false; if true, define a group 
     ); 

     $installer->addAttribute('catalog_product','attriute_code',$data) 

     $installer->endSetup(); 

を設定モジュールの属性のインストールの実施例です。

1

をいただければ幸いです。この例の$thisは、インストーラクラスを意味します。

https://bitbucket.org/alexsiri7/qbmagemoduleshellを使用してモジュールとインストーラを作成し、そこにコードを追加することができます。そのツールは、私が開発したモジュール作成者です。

5

これは簡単な1ライナーです。インストールスクリプトでMage_Eav_Model_Entity_Setupを拡張し、インストーラで次のようなものを使用してください:

$installer = $this; 
/* @var $installer Mage_Eav_Model_Entity_Setup */ 

$installer->startSetup(); 
$installer->addAttributeSet(Mage_Catalog_Model_Product::ENTITY, 'New Attribute Set Name'); 
$installer->endSetup();