2011-12-23 7 views
-1

は既にこのタスクについて何かを見つけましたが、ちょうどSQL Querysこれらのスニペット、それは、のような最高の属性ID /最高attributeoptionのIDにはプログラムによる最大ATTRIBUTE_IDを取得し、最大で(SQLクエリなし)APIを経由してattributeoption_id

を得ることが可能です私は属性を作成するときに、attribute_id 11を取得しましたが、今ではその属性を作成せずにそのツールを使用しています - >書き込みではなくすべての属性を読み込みます。 Magento APIにmysqlデータベースをクエリーしないでください。

より具体的にする: 私は何をしたいことはすべてattribute_idsを取得し、すべての既存の属性ものの反復するためにある、一つの属性のすべての属性オプションの出力

ためattribute_namesが同じ

は何も見つかりませんでしたおそらく、私はインターネット上の間違った場所を見てきました。誰かが私を助けてくれることを願います。

+1

ありがとうございました!大文字を使用してください。ほんの数分しかかかりません。 –

+0

データベースに照会せずにデータベース値を取得することはできません。 –

+0

Magento APIメソッド(データベース自体を照会する)を使用してカスタムクエリメソッドを使用するのではなく、 –

答えて

0

Magentoの属性は、モデル 'eav/entity_attribute'で表されます。次のコードは、それらにアクセスする方法を示しています。あなたが探していたことを願っています。

/* 
* Initialize Magento. This assumes that we're running the script from the root 
* directory of a magento installation. Obviously if you're writing an extension 
* you don't need this. 
*/ 
require_once 'app/Mage.php'; 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
Mage::getSingleton("core/session")->setUser(Mage::getModel("admin/user")->load(0)); 
umask(0); 

/* 
* Attributes are represented by the eav/entity_attribute model 
*/ 
$model = Mage::getModel("eav/entity_attribute"); 
foreach($model->getCollection() as $attr){ 
    //$attr is a Mage_Eav_Model_Entity_Attribute (see Mage_Eav_Model_Entity_Attribute_Abstract for 
    //most of the action). 

    //Replace with your attribute processing code 
    echo "id={$attr->getId()}, code={$attr->getAttributeCode()},label={$attr->getFrontendLabel()}\n"; 

    //Check to see if the attribute has options 
    if($attr->usesSource()){ 
     echo " attribute options"; 

     //process the options. 
     foreach($attr->getSource()->getAllOptions() as $option){ 
      //Replace with your option processing code. 

      $label = $option['label']; 
      $value = $option['value']; 

      //Normally label is a string and value is an int. However, 
      //some options have sub options. In this case value is like 
      //array(array('label'=>'Label', 'value'=>'...'), ...) 
      if(is_array($value)){ 
       $value = print_r($value, true); 
      } 
      echo " $label => $value\n"; 
     } 
    } 
} 

あなたは属性で行うことができますいくつかの他のきちんとしたもの:

//Load an attribute by code/name 
Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'price'); 
//Get the attribute set for an attribute 
$setId = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'price')->getAttributeSetId(); 
$set = Mage::getModel('eav/entity_attribute_set')->load($setId); 

アプリ/コード/コア/メイジ、アプリ/コード/コア/メイジ/ EAV /モデル/エンティティ/ Attribute.phpを参照してください。 /Eav/Model/Entity/Attribute/Abstract.php

+0

スニペットjjmに感謝しますが、それは多分、私はマゼンタ自体の中にコードを書いているわけではないので、それはうまくいきません。コネクターが多いので、セッションは機能しません。私は単純に必要なのは次のような出力です:magentosのapp/Mage.phpを使って、magento外のPHPファイルで "maxoptionsid = x" "maxattribid = y"を出力します。 –

+0

これはmagentoの外で動作します。 cd/path/to/magento(アプリケーションディレクトリが必要です)、php/path/to/scriptだけです。それはセッションを必要としません - 上のビットはそれをコマンドラインプログラムとして動作させます。 – jjm

関連する問題