2012-10-15 4 views
0

나는 Magento 속성을 프로그래밍 방식으로 작성하여 CSV에서 데이터를 가져 오는 스크립트를 작성하고 있습니다. 내가 CSV에서 데이터를 가져 오는 올바른 실제 루프를 가지고 있는지 확신 할 수 없다 - 로직에 대한 전문가의 조언이 필요 했습니까? 메신저 내가 추측 -CSV에서 속성을 루프로 만들기

<?php 
$fh = fopen("attributes.csv", "r"); 
$i = 0; 
while (($l = fgetcsv($fh, 1024, ",")) !== FALSE) { 

    $i++; 
    if($i == 1) continue; //ignoring the headers, so skip row 0 
    $data['label'] = trim($l[2]); 
    $data['input'] = trim($l[3]); 
    $data['type'] = trim($l[2]); 

    //Create the attribute 

    $data=array(
'type'=>$data['type'], 
'input'=>'text', 
'label'=>$data['label'], 
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
'is_required'=>'0', 
'is_comparable'=>'0', 
'is_searchable'=>'0', 
'is_unique'=>'1', 
'is_configurable'=>'1', 
'use_defined'=>'1' 
); 

$model->addAttribute('catalog_product','test_attribute',$data); 

} 

?> 

는 기본적으로 내가 그냥이 CSV에서 속성 데이터를 잡고 싶어하고, CSV의 각 행의 레이블과 CSV에 지정된 이름을 사용하여 (그것을 만드는 코드를 실행 루프에서 뭔가를 분명 실종? (정말 내가 뭘하는지 학습!)를 CSV-내용이 분실 있도록

+0

이 스크립트를 실행하면 어떻게됩니까? ttp : //alanstorm.com/magento_attribute_migration_generator, 도움이됩니다. – Zyava

답변

1

당신은, CSV의 값을 삽입 한 후, 각 루프에서 $data 배열을 다시 설정합니다.이

시도
$fh = fopen("attributes.csv", "r"); 
$i = 0; 
$attributes=array(); //!! 
while (($l = fgetcsv($fh, 1024, ",")) !== FALSE) { 

    $i++; 
    if($i == 1) continue; //ignoring the headers, so skip row 0 

    $data=array(); 

    $data['label'] = trim($l[2]); 
    $data['input'] = trim($l[3]); 
    $data['type'] = trim($l[2]); 

    $data['global']=Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL; 
    $data['is_required']='0'; 
    $data['is_comparable']='0'; 
    $data['is_searchable']='0'; 
    $data['is_unique']='1'; 
    $data['is_configurable']='1'; 
    $data['use_defined']='1'; 

    //insert $data to the attributes array 
    $attributes[]=$data; 
    //or 
    $model->addAttribute('catalog_product','test_attribute',$data); 
}